Update LikeService

This commit is contained in:
Daniel Supernault 2022-09-21 05:34:39 -06:00
parent 842f40a1b5
commit 20d9f8b890
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -10,20 +10,53 @@ use App\Like;
class LikeService { class LikeService {
const CACHE_KEY = 'pf:services:likes:ids:'; const CACHE_KEY = 'pf:services:likes:ids:';
const CACHE_SET_KEY = 'pf:services:likes:set:';
public static function add($profileId, $statusId) public static function add($profileId, $statusId)
{ {
$key = self::CACHE_KEY . $profileId . ':' . $statusId; $key = self::CACHE_KEY . $profileId . ':' . $statusId;
Cache::increment('pf:services:likes:count:'.$statusId); Cache::increment('pf:services:likes:count:'.$statusId);
Cache::forget('pf:services:likes:liked_by:'.$statusId); Cache::forget('pf:services:likes:liked_by:'.$statusId);
self::setAdd($profileId, $statusId);
return Cache::put($key, true, 86400); return Cache::put($key, true, 86400);
} }
public static function setAdd($profileId, $statusId)
{
if(self::setCount($profileId) > 400) {
if(config('database.redis.client') === 'phpredis') {
Redis::zpopmin(self::CACHE_SET_KEY . $id);
}
}
return Redis::zadd(self::CACHE_SET_KEY . $profileId, $statusId, $statusId);
}
public static function setCount($id)
{
return Redis::zcard(self::CACHE_SET_KEY . $id);
}
public static function setRem($profileId, $val)
{
return Redis::zrem(self::CACHE_SET_KEY . $profileId, $val);
}
public static function get($profileId, $start = 0, $stop = 10)
{
if($stop > 100) {
$stop = 100;
}
return Redis::zrevrange(self::CACHE_SET_KEY . $profileId, $start, $stop);
}
public static function remove($profileId, $statusId) public static function remove($profileId, $statusId)
{ {
$key = self::CACHE_KEY . $profileId . ':' . $statusId; $key = self::CACHE_KEY . $profileId . ':' . $statusId;
Cache::decrement('pf:services:likes:count:'.$statusId); Cache::decrement('pf:services:likes:count:'.$statusId);
Cache::forget('pf:services:likes:liked_by:'.$statusId); Cache::forget('pf:services:likes:liked_by:'.$statusId);
self::setRem($profileId, $statusId);
return Cache::put($key, false, 86400); return Cache::put($key, false, 86400);
} }