Merge pull request #5301 from pixelfed/staging

Update ReblogService, fix cache sync issues
This commit is contained in:
daniel 2024-09-17 02:50:18 -06:00 committed by GitHub
commit 5be19ff657
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,35 +2,124 @@
namespace App\Services; namespace App\Services;
use App\Status;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use App\Status; use Illuminate\Support\Lottery;
class ReblogService class ReblogService
{ {
const CACHE_KEY = 'pf:services:reblogs:'; const CACHE_KEY = 'pf:services:reblogs:';
const REBLOGS_KEY = 'pf:services:reblogs:v1:post:'; const REBLOGS_KEY = 'pf:services:reblogs:v1:post:';
const COLDBOOT_KEY = 'pf:services:reblogs:v1:post_:'; const COLDBOOT_KEY = 'pf:services:reblogs:v1:post_:';
const CACHE_SKIP_KEY = 'pf:services:reblogs:skip_empty_check:';
public static function get($profileId, $statusId) public static function get($profileId, $statusId)
{
return Lottery::odds(1, 20)
->winner(fn () => self::getFromDatabaseCheck($profileId, $statusId))
->loser(fn () => self::getFromRedis($profileId, $statusId))
->choose();
}
public static function getFromDatabaseCheck($profileId, $statusId)
{ {
if (! Redis::zcard(self::CACHE_KEY.$profileId)) { if (! Redis::zcard(self::CACHE_KEY.$profileId)) {
if (Cache::has(self::CACHE_SKIP_KEY.$profileId)) {
return false; return false;
} else {
self::warmCache($profileId);
sleep(1);
return self::getFromRedis($profileId, $statusId);
}
}
$minId = SnowflakeService::byDate(now()->subMonths(12));
if ($minId > $statusId) {
return Redis::zscore(self::CACHE_KEY.$profileId, $statusId) != null;
}
$cachedRes = (bool) Redis::zscore(self::CACHE_KEY.$profileId, $statusId) != null;
$databaseRes = (bool) self::getFromDatabase($profileId, $statusId);
if ($cachedRes === $databaseRes) {
return $cachedRes;
}
self::warmCache($profileId);
sleep(1);
return self::getFromDatabase($profileId, $statusId);
}
public static function getFromRedis($profileId, $statusId)
{
if (! Redis::zcard(self::CACHE_KEY.$profileId)) {
if (Cache::has(self::CACHE_SKIP_KEY.$profileId)) {
return false;
} else {
self::warmCache($profileId);
sleep(1);
return self::getFromDatabase($profileId, $statusId);
}
} }
return Redis::zscore(self::CACHE_KEY.$profileId, $statusId) != null; return Redis::zscore(self::CACHE_KEY.$profileId, $statusId) != null;
} }
public static function getFromDatabase($profileId, $statusId)
{
return Status::whereProfileId($profileId)
->where('reblog_of_id', $statusId)
->exists();
}
public static function add($profileId, $statusId) public static function add($profileId, $statusId)
{ {
return Redis::zadd(self::CACHE_KEY.$profileId, $statusId, $statusId); return Redis::zadd(self::CACHE_KEY.$profileId, $statusId, $statusId);
} }
public static function count($profileId)
{
return Redis::zcard(self::CACHE_KEY.$profileId);
}
public static function del($profileId, $statusId) public static function del($profileId, $statusId)
{ {
return Redis::zrem(self::CACHE_KEY.$profileId, $statusId); return Redis::zrem(self::CACHE_KEY.$profileId, $statusId);
} }
public static function getWarmCacheCount($profileId)
{
$minId = SnowflakeService::byDate(now()->subMonths(12));
return Status::where('id', '>', $minId)
->whereProfileId($profileId)
->whereNotNull('reblog_of_id')
->count();
}
public static function warmCache($profileId)
{
Redis::del(self::CACHE_KEY.$profileId);
$minId = SnowflakeService::byDate(now()->subMonths(12));
foreach (
Status::where('id', '>', $minId)
->whereProfileId($profileId)
->whereNotNull('reblog_of_id')
->lazy() as $post
) {
self::add($profileId, $post->reblog_of_id);
}
Cache::put(self::CACHE_SKIP_KEY.$profileId, 1, now()->addHours(24));
}
public static function getPostReblogs($id, $start = 0, $stop = 10) public static function getPostReblogs($id, $start = 0, $stop = 10)
{ {
if (! Redis::zcard(self::REBLOGS_KEY.$id)) { if (! Redis::zcard(self::REBLOGS_KEY.$id)) {
@ -45,6 +134,7 @@ class ReblogService
}); });
}); });
} }
return Redis::zrange(self::REBLOGS_KEY.$id, $start, $stop); return Redis::zrange(self::REBLOGS_KEY.$id, $start, $stop);
} }