2019-06-11 02:11:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-12-11 06:04:03 +00:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
2019-06-11 02:11:52 +00:00
|
|
|
use App\{
|
|
|
|
Profile,
|
|
|
|
Status,
|
|
|
|
UserFilter
|
|
|
|
};
|
|
|
|
|
|
|
|
class PublicTimelineService {
|
|
|
|
|
|
|
|
const CACHE_KEY = 'pf:services:timeline:public';
|
|
|
|
|
|
|
|
public static function get($start = 0, $stop = 10)
|
|
|
|
{
|
|
|
|
if($stop > 100) {
|
|
|
|
$stop = 100;
|
|
|
|
}
|
2021-07-06 08:01:24 +00:00
|
|
|
|
|
|
|
return Redis::zrevrange(self::CACHE_KEY, $start, $stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getRankedMaxId($start = null, $limit = 10)
|
|
|
|
{
|
|
|
|
if(!$start) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY, $start, '-inf', [
|
|
|
|
'withscores' => true,
|
|
|
|
'limit' => [1, $limit]
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getRankedMinId($end = null, $limit = 10)
|
|
|
|
{
|
|
|
|
if(!$end) {
|
|
|
|
return [];
|
2019-06-11 02:11:52 +00:00
|
|
|
}
|
2021-07-06 08:01:24 +00:00
|
|
|
|
|
|
|
return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY, '+inf', $end, [
|
|
|
|
'withscores' => true,
|
|
|
|
'limit' => [0, $limit]
|
|
|
|
]));
|
2019-06-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function add($val)
|
|
|
|
{
|
2021-09-08 03:07:20 +00:00
|
|
|
if(self::count() > 400) {
|
|
|
|
if(config('database.redis.client') === 'phpredis') {
|
|
|
|
Redis::zpopmin(self::CACHE_KEY);
|
|
|
|
}
|
2021-07-06 08:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Redis::zadd(self::CACHE_KEY, $val, $val);
|
2019-06-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function rem($val)
|
|
|
|
{
|
|
|
|
return Redis::zrem(self::CACHE_KEY, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function del($val)
|
|
|
|
{
|
|
|
|
return self::rem($val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function count()
|
|
|
|
{
|
2021-07-06 08:01:24 +00:00
|
|
|
return Redis::zcard(self::CACHE_KEY);
|
2019-06-11 02:11:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 11:43:42 +00:00
|
|
|
public static function deleteByProfileId($profileId)
|
|
|
|
{
|
|
|
|
$res = Redis::zrange(self::CACHE_KEY, 0, '-1');
|
|
|
|
if(!$res) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach($res as $postId) {
|
|
|
|
$s = StatusService::get($postId);
|
|
|
|
if(!$s) {
|
|
|
|
self::rem($postId);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if($s['account']['id'] == $profileId) {
|
|
|
|
self::rem($postId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-11 02:11:52 +00:00
|
|
|
public static function warmCache($force = false, $limit = 100)
|
|
|
|
{
|
|
|
|
if(self::count() == 0 || $force == true) {
|
2022-11-28 12:13:12 +00:00
|
|
|
$hideNsfw = config('instance.hide_nsfw_on_public_feeds');
|
2021-07-08 23:33:51 +00:00
|
|
|
Redis::del(self::CACHE_KEY);
|
2023-05-23 07:29:22 +00:00
|
|
|
$minId = SnowflakeService::byDate(now()->subDays(14));
|
|
|
|
$ids = Status::where('id', '>', $minId)
|
|
|
|
->whereNull(['uri', 'in_reply_to_id', 'reblog_of_id'])
|
2022-11-28 12:13:12 +00:00
|
|
|
->when($hideNsfw, function($q, $hideNsfw) {
|
|
|
|
return $q->where('is_nsfw', false);
|
|
|
|
})
|
2019-11-23 05:40:29 +00:00
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
2019-06-11 02:11:52 +00:00
|
|
|
->whereScope('public')
|
2021-07-06 08:01:24 +00:00
|
|
|
->orderByDesc('id')
|
2019-06-11 02:11:52 +00:00
|
|
|
->limit($limit)
|
|
|
|
->pluck('id');
|
|
|
|
foreach($ids as $id) {
|
|
|
|
self::add($id);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-07-02 07:21:21 +00:00
|
|
|
}
|