mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-25 15:55:22 +00:00
Update StatusHashtagService, use more efficient cached count
This commit is contained in:
parent
e68fe64ffc
commit
592c84125c
1 changed files with 78 additions and 77 deletions
|
@ -2,96 +2,97 @@
|
|||
|
||||
namespace App\Services;
|
||||
|
||||
use Cache;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use App\{Status, StatusHashtag};
|
||||
use App\Transformer\Api\StatusHashtagTransformer;
|
||||
use App\Hashtag;
|
||||
use App\Status;
|
||||
use App\StatusHashtag;
|
||||
use App\Transformer\Api\HashtagTransformer;
|
||||
use League\Fractal;
|
||||
use League\Fractal\Serializer\ArraySerializer;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
|
||||
class StatusHashtagService {
|
||||
class StatusHashtagService
|
||||
{
|
||||
const CACHE_KEY = 'pf:services:status-hashtag:collection:';
|
||||
|
||||
const CACHE_KEY = 'pf:services:status-hashtag:collection:';
|
||||
public static function get($id, $page = 1, $stop = 9)
|
||||
{
|
||||
if ($page > 20) {
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function get($id, $page = 1, $stop = 9)
|
||||
{
|
||||
if($page > 20) {
|
||||
return [];
|
||||
}
|
||||
$pid = request()->user() ? request()->user()->profile_id : false;
|
||||
$filtered = $pid ? UserFilterService::filters($pid) : [];
|
||||
|
||||
$pid = request()->user() ? request()->user()->profile_id : false;
|
||||
$filtered = $pid ? UserFilterService::filters($pid) : [];
|
||||
return StatusHashtag::whereHashtagId($id)
|
||||
->whereStatusVisibility('public')
|
||||
->skip($stop)
|
||||
->latest()
|
||||
->take(9)
|
||||
->pluck('status_id')
|
||||
->map(function ($i, $k) use ($id) {
|
||||
return self::getStatus($i, $id);
|
||||
})
|
||||
->filter(function ($i) use ($filtered) {
|
||||
return isset($i['status']) &&
|
||||
! empty($i['status']) && ! in_array($i['status']['account']['id'], $filtered) &&
|
||||
isset($i['status']['media_attachments']) &&
|
||||
! empty($i['status']['media_attachments']);
|
||||
})
|
||||
->values();
|
||||
}
|
||||
|
||||
return StatusHashtag::whereHashtagId($id)
|
||||
->whereStatusVisibility('public')
|
||||
->skip($stop)
|
||||
->latest()
|
||||
->take(9)
|
||||
->pluck('status_id')
|
||||
->map(function ($i, $k) use ($id) {
|
||||
return self::getStatus($i, $id);
|
||||
})
|
||||
->filter(function ($i) use($filtered) {
|
||||
return isset($i['status']) &&
|
||||
!empty($i['status']) && !in_array($i['status']['account']['id'], $filtered) &&
|
||||
isset($i['status']['media_attachments']) &&
|
||||
!empty($i['status']['media_attachments']);
|
||||
})
|
||||
->values();
|
||||
}
|
||||
public static function coldGet($id, $start = 0, $stop = 2000)
|
||||
{
|
||||
$stop = $stop > 2000 ? 2000 : $stop;
|
||||
$ids = StatusHashtag::whereHashtagId($id)
|
||||
->whereStatusVisibility('public')
|
||||
->whereHas('media')
|
||||
->latest()
|
||||
->skip($start)
|
||||
->take($stop)
|
||||
->pluck('status_id');
|
||||
foreach ($ids as $key) {
|
||||
self::set($id, $key);
|
||||
}
|
||||
|
||||
public static function coldGet($id, $start = 0, $stop = 2000)
|
||||
{
|
||||
$stop = $stop > 2000 ? 2000 : $stop;
|
||||
$ids = StatusHashtag::whereHashtagId($id)
|
||||
->whereStatusVisibility('public')
|
||||
->whereHas('media')
|
||||
->latest()
|
||||
->skip($start)
|
||||
->take($stop)
|
||||
->pluck('status_id');
|
||||
foreach($ids as $key) {
|
||||
self::set($id, $key);
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
|
||||
public static function set($key, $val)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
public static function set($key, $val)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public static function del($key)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
public static function del($key)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public static function count($id)
|
||||
{
|
||||
$key = 'pf:services:status-hashtag:count:' . $id;
|
||||
$ttl = now()->addMinutes(5);
|
||||
return Cache::remember($key, $ttl, function() use($id) {
|
||||
return StatusHashtag::whereHashtagId($id)->has('media')->count();
|
||||
});
|
||||
}
|
||||
public static function count($id)
|
||||
{
|
||||
$cc = Hashtag::find($id);
|
||||
if (! $cc) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function getStatus($statusId, $hashtagId)
|
||||
{
|
||||
return ['status' => StatusService::get($statusId)];
|
||||
}
|
||||
return $cc->cached_count ?? 0;
|
||||
}
|
||||
|
||||
public static function statusTags($statusId)
|
||||
{
|
||||
$status = Status::with('hashtags')->find($statusId);
|
||||
if(!$status) {
|
||||
return [];
|
||||
}
|
||||
public static function getStatus($statusId, $hashtagId)
|
||||
{
|
||||
return ['status' => StatusService::get($statusId)];
|
||||
}
|
||||
|
||||
$fractal = new Fractal\Manager();
|
||||
$fractal->setSerializer(new ArraySerializer());
|
||||
$resource = new Fractal\Resource\Collection($status->hashtags, new HashtagTransformer());
|
||||
return $fractal->createData($resource)->toArray();
|
||||
}
|
||||
public static function statusTags($statusId)
|
||||
{
|
||||
$status = Status::with('hashtags')->find($statusId);
|
||||
if (! $status) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$fractal = new Fractal\Manager();
|
||||
$fractal->setSerializer(new ArraySerializer());
|
||||
$resource = new Fractal\Resource\Collection($status->hashtags, new HashtagTransformer());
|
||||
|
||||
return $fractal->createData($resource)->toArray();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue