2022-01-29 05:06:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2022-03-11 06:34:34 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-01-29 05:06:43 +00:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
2022-03-11 06:34:34 +00:00
|
|
|
use App\Status;
|
2022-01-29 05:06:43 +00:00
|
|
|
|
|
|
|
class ReblogService
|
|
|
|
{
|
|
|
|
const CACHE_KEY = 'pf:services:reblogs:';
|
2022-03-11 06:58:47 +00:00
|
|
|
const REBLOGS_KEY = 'pf:services:reblogs:v1:post:';
|
|
|
|
const COLDBOOT_KEY = 'pf:services:reblogs:v1:post_:';
|
2022-01-29 05:06:43 +00:00
|
|
|
|
|
|
|
public static function get($profileId, $statusId)
|
|
|
|
{
|
|
|
|
if (!Redis::zcard(self::CACHE_KEY . $profileId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redis::zscore(self::CACHE_KEY . $profileId, $statusId) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function add($profileId, $statusId)
|
|
|
|
{
|
|
|
|
return Redis::zadd(self::CACHE_KEY . $profileId, $statusId, $statusId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function del($profileId, $statusId)
|
|
|
|
{
|
|
|
|
return Redis::zrem(self::CACHE_KEY . $profileId, $statusId);
|
|
|
|
}
|
2022-03-11 06:34:34 +00:00
|
|
|
|
|
|
|
public static function getPostReblogs($id, $start = 0, $stop = 10)
|
|
|
|
{
|
|
|
|
if(!Redis::zcard(self::REBLOGS_KEY . $id)) {
|
|
|
|
return Cache::remember(self::COLDBOOT_KEY . $id, 86400, function() use($id) {
|
|
|
|
return Status::whereReblogOfId($id)
|
|
|
|
->pluck('id')
|
|
|
|
->each(function($reblog) use($id) {
|
|
|
|
self::addPostReblog($id, $reblog);
|
|
|
|
})
|
|
|
|
->map(function($reblog) {
|
|
|
|
return (string) $reblog;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Redis::zrange(self::REBLOGS_KEY . $id, $start, $stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function addPostReblog($parentId, $reblogId)
|
|
|
|
{
|
2022-03-11 06:55:32 +00:00
|
|
|
$pid = intval($parentId);
|
|
|
|
$id = intval($reblogId);
|
|
|
|
if($pid && $id) {
|
2022-03-11 07:00:47 +00:00
|
|
|
return Redis::zadd(self::REBLOGS_KEY . $pid, $id, $id);
|
2022-03-11 06:55:32 +00:00
|
|
|
}
|
2022-03-11 06:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function removePostReblog($parentId, $reblogId)
|
|
|
|
{
|
2022-03-11 06:55:32 +00:00
|
|
|
$pid = intval($parentId);
|
|
|
|
$id = intval($reblogId);
|
|
|
|
if($pid && $id) {
|
|
|
|
return Redis::zrem(self::REBLOGS_KEY . $pid, $id);
|
|
|
|
}
|
2022-03-11 06:34:34 +00:00
|
|
|
}
|
2022-01-29 05:06:43 +00:00
|
|
|
}
|