Fix StoryIndexService

pull/7171/head
Daniel Supernault 2 weeks ago
parent 13aa36efb4
commit 57e7eef082
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1

@ -6,6 +6,7 @@ use App\Jobs\HomeFeedPipeline\FeedFollowPipeline;
use App\Jobs\HomeFeedPipeline\FeedUnfollowPipeline;
use App\Models\Follower;
use App\Services\FollowerService;
use App\Services\StoryIndexService;
use Illuminate\Support\Facades\Cache;
class FollowerObserver
@ -22,6 +23,7 @@ class FollowerObserver
}
FollowerService::add($follower->profile_id, $follower->following_id);
app(StoryIndexService::class)->addFollowing($follower->profile_id, $follower->following_id);
FeedFollowPipeline::dispatch($follower->profile_id, $follower->following_id)->onQueue('follow');
}
@ -33,6 +35,7 @@ class FollowerObserver
public function deleted(Follower $follower)
{
FollowerService::remove($follower->profile_id, (string) $follower->following_id);
app(StoryIndexService::class)->removeFollowing($follower->profile_id, $follower->following_id);
FeedUnfollowPipeline::dispatch($follower->profile_id, $follower->following_id)->onQueue('feed');
}
@ -44,5 +47,6 @@ class FollowerObserver
public function forceDeleted(Follower $follower)
{
FollowerService::remove($follower->profile_id, (string) $follower->following_id);
app(StoryIndexService::class)->removeFollowing($follower->profile_id, $follower->following_id);
}
}

@ -176,7 +176,16 @@ class StoryIndexService
$path = $story->path;
Redis::pipeline(function ($pipe) use (
$author, $sid, $score, $ttl, $duration, $overlays, $viewCount, $createdIso, $type, $path
$author,
$sid,
$score,
$ttl,
$duration,
$overlays,
$viewCount,
$createdIso,
$type,
$path
) {
$keyStory = $this->storyKey($sid);
$keyAuth = $this->authorKey($author);
@ -235,6 +244,36 @@ class StoryIndexService
Redis::expire($key, $finalTtl);
}
/**
* Keep the cached following set in sync when a follow is created.
* Only touches the key if it already exists; otherwise the next carousel
* fetch hydrates from SQL and picks up the new follow anyway.
*/
public function addFollowing(int $followerId, int $followingId): void
{
$key = "following:{$followerId}";
if (! Redis::exists($key)) {
return;
}
Redis::sadd($key, (string) $followingId);
}
/**
* Keep the cached following set in sync when a follow is removed.
*/
public function removeFollowing(int $followerId, int $followingId): void
{
$key = "following:{$followerId}";
if (! Redis::exists($key)) {
return;
}
Redis::srem($key, (string) $followingId);
}
public function rebuildIndex(): array
{
$lockKey = $this->rebuildLockKey();
@ -331,7 +370,6 @@ class StoryIndexService
'message' => 'Story index and seen data rebuilt successfully',
'stats' => $stats,
];
} finally {
Redis::del($lockKey);
}
@ -657,7 +695,6 @@ class StoryIndexService
if (! $hasResults) {
Redis::pipeline(function ($pipe) use ($followingKey) {
$pipe->sadd($followingKey, '__empty__');
$pipe->srem($followingKey, '__empty__');
$pipe->expire($followingKey, 3600);
});
} else {

Loading…
Cancel
Save