forked from mirror/pixelfed
Update LikeService
This commit is contained in:
parent
117b8410eb
commit
447e44e5ac
1 changed files with 14 additions and 28 deletions
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use App\Util\ActivityPub\Helpers;
|
use App\Util\ActivityPub\Helpers;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Redis;
|
use Illuminate\Support\Facades\Redis;
|
||||||
use App\Like;
|
use App\Like;
|
||||||
|
|
||||||
|
@ -10,42 +11,27 @@ class LikeService {
|
||||||
|
|
||||||
const CACHE_KEY = 'pf:services:likes:ids:';
|
const CACHE_KEY = 'pf:services:likes:ids:';
|
||||||
|
|
||||||
public static function getUser($profile_id)
|
public static function add($profileId, $statusId)
|
||||||
{
|
{
|
||||||
return self::get($profile_id);
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
||||||
|
$ttl = now()->addHours(2);
|
||||||
|
return Cache::put($key, true, $ttl);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function get($profile_id)
|
public static function remove($profileId, $statusId)
|
||||||
{
|
{
|
||||||
$key = self::CACHE_KEY . $profile_id;
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
||||||
if(Redis::zcard($key) == 0) {
|
$ttl = now()->addHours(2);
|
||||||
self::warmCache($profile_id);
|
return Cache::put($key, false, $ttl);
|
||||||
} else {
|
|
||||||
return Redis::zrevrange($key, 0, 40);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function set($profile_id, $status_id)
|
|
||||||
{
|
|
||||||
$key = self::CACHE_KEY . $profile_id;
|
|
||||||
Redis::zadd($key, $status_id, $status_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function warmCache($profile_id)
|
|
||||||
{
|
|
||||||
Like::select('id', 'profile_id', 'status_id')
|
|
||||||
->whereProfileId($profile_id)
|
|
||||||
->latest()
|
|
||||||
->get()
|
|
||||||
->each(function($like) use ($profile_id) {
|
|
||||||
self::set($profile_id, $like->status_id);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function liked($profileId, $statusId)
|
public static function liked($profileId, $statusId)
|
||||||
{
|
{
|
||||||
$key = self::CACHE_KEY . $profileId;
|
$key = self::CACHE_KEY . $profileId . ':' . $statusId;
|
||||||
return (bool) Redis::zrank($key, $statusId);
|
$ttl = now()->addMinutes(30);
|
||||||
|
return Cache::remember($key, $ttl, function() use($profileId, $statusId) {
|
||||||
|
return Like::whereProfileId($profileId)->whereStatusId($statusId)->exists();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function likedBy($status)
|
public static function likedBy($status)
|
||||||
|
|
Loading…
Reference in a new issue