From 59b643789fae7ba44692acbfdb896fbb0e48d3be Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 22 Jul 2023 03:50:23 -0600 Subject: [PATCH 1/2] Update StatusService, reduce cache ttl from 7 days to 6 hours --- app/Services/StatusService.php | 294 ++++++++++++++++----------------- 1 file changed, 147 insertions(+), 147 deletions(-) diff --git a/app/Services/StatusService.php b/app/Services/StatusService.php index 99bcee2b0..bdf2f31db 100644 --- a/app/Services/StatusService.php +++ b/app/Services/StatusService.php @@ -14,173 +14,173 @@ use League\Fractal\Pagination\IlluminatePaginatorAdapter; class StatusService { - const CACHE_KEY = 'pf:services:status:'; + const CACHE_KEY = 'pf:services:status:'; - public static function key($id, $publicOnly = true) - { - $p = $publicOnly ? 'pub:' : 'all:'; - return self::CACHE_KEY . $p . $id; - } + public static function key($id, $publicOnly = true) + { + $p = $publicOnly ? 'pub:' : 'all:'; + return self::CACHE_KEY . $p . $id; + } - public static function get($id, $publicOnly = true) - { - return Cache::remember(self::key($id, $publicOnly), now()->addDays(7), function() use($id, $publicOnly) { - if($publicOnly) { - $status = Status::whereScope('public')->find($id); - } else { - $status = Status::whereIn('scope', ['public', 'private', 'unlisted', 'group'])->find($id); - } - if(!$status) { - return null; - } - $fractal = new Fractal\Manager(); - $fractal->setSerializer(new ArraySerializer()); - $resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer()); - return $fractal->createData($resource)->toArray(); - }); - } + public static function get($id, $publicOnly = true) + { + return Cache::remember(self::key($id, $publicOnly), 21600, function() use($id, $publicOnly) { + if($publicOnly) { + $status = Status::whereScope('public')->find($id); + } else { + $status = Status::whereIn('scope', ['public', 'private', 'unlisted', 'group'])->find($id); + } + if(!$status) { + return null; + } + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer()); + return $fractal->createData($resource)->toArray(); + }); + } - public static function getMastodon($id, $publicOnly = true) - { - $status = self::get($id, $publicOnly); - if(!$status) { - return null; - } + public static function getMastodon($id, $publicOnly = true) + { + $status = self::get($id, $publicOnly); + if(!$status) { + return null; + } - if(!isset($status['account'])) { - return null; - } + if(!isset($status['account'])) { + return null; + } $status['replies_count'] = $status['reply_count']; - if(config('exp.emc') == false) { - return $status; - } + if(config('exp.emc') == false) { + return $status; + } - unset( - $status['_v'], - $status['comments_disabled'], - $status['content_text'], - $status['gid'], - $status['label'], - $status['liked_by'], - $status['local'], - $status['parent'], - $status['pf_type'], - $status['place'], - $status['replies'], - $status['reply_count'], - $status['shortcode'], - $status['taggedPeople'], - $status['thread'], - $status['pinned'], - $status['account']['header_bg'], - $status['account']['is_admin'], - $status['account']['last_fetched_at'], - $status['account']['local'], - $status['account']['location'], - $status['account']['note_text'], - $status['account']['pronouns'], - $status['account']['website'], - $status['media_attachments'], - ); - $status['account']['avatar_static'] = $status['account']['avatar']; - $status['account']['bot'] = false; - $status['account']['emojis'] = []; - $status['account']['fields'] = []; - $status['account']['header'] = url('/storage/headers/missing.png'); - $status['account']['header_static'] = url('/storage/headers/missing.png'); - $status['account']['last_status_at'] = null; + unset( + $status['_v'], + $status['comments_disabled'], + $status['content_text'], + $status['gid'], + $status['label'], + $status['liked_by'], + $status['local'], + $status['parent'], + $status['pf_type'], + $status['place'], + $status['replies'], + $status['reply_count'], + $status['shortcode'], + $status['taggedPeople'], + $status['thread'], + $status['pinned'], + $status['account']['header_bg'], + $status['account']['is_admin'], + $status['account']['last_fetched_at'], + $status['account']['local'], + $status['account']['location'], + $status['account']['note_text'], + $status['account']['pronouns'], + $status['account']['website'], + $status['media_attachments'], + ); + $status['account']['avatar_static'] = $status['account']['avatar']; + $status['account']['bot'] = false; + $status['account']['emojis'] = []; + $status['account']['fields'] = []; + $status['account']['header'] = url('/storage/headers/missing.png'); + $status['account']['header_static'] = url('/storage/headers/missing.png'); + $status['account']['last_status_at'] = null; - $status['media_attachments'] = array_values(MediaService::getMastodon($status['id'])); - $status['muted'] = false; - $status['reblogged'] = false; + $status['media_attachments'] = array_values(MediaService::getMastodon($status['id'])); + $status['muted'] = false; + $status['reblogged'] = false; - return $status; - } + return $status; + } - public static function getState($id, $pid) - { - $status = self::get($id, false); + public static function getState($id, $pid) + { + $status = self::get($id, false); - if(!$status) { - return [ - 'liked' => false, - 'shared' => false, - 'bookmarked' => false - ]; - } + if(!$status) { + return [ + 'liked' => false, + 'shared' => false, + 'bookmarked' => false + ]; + } - return [ - 'liked' => LikeService::liked($pid, $id), - 'shared' => self::isShared($id, $pid), - 'bookmarked' => self::isBookmarked($id, $pid) - ]; - } + return [ + 'liked' => LikeService::liked($pid, $id), + 'shared' => self::isShared($id, $pid), + 'bookmarked' => self::isBookmarked($id, $pid) + ]; + } - public static function getFull($id, $pid, $publicOnly = true) - { - $res = self::get($id, $publicOnly); - if(!$res || !isset($res['account']) || !isset($res['account']['id'])) { - return $res; - } - $res['relationship'] = RelationshipService::get($pid, $res['account']['id']); - return $res; - } + public static function getFull($id, $pid, $publicOnly = true) + { + $res = self::get($id, $publicOnly); + if(!$res || !isset($res['account']) || !isset($res['account']['id'])) { + return $res; + } + $res['relationship'] = RelationshipService::get($pid, $res['account']['id']); + return $res; + } - public static function getDirectMessage($id) - { - $status = Status::whereScope('direct')->find($id); + public static function getDirectMessage($id) + { + $status = Status::whereScope('direct')->find($id); - if(!$status) { - return null; - } + if(!$status) { + return null; + } - $fractal = new Fractal\Manager(); - $fractal->setSerializer(new ArraySerializer()); - $resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer()); - return $fractal->createData($resource)->toArray(); - } + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Item($status, new StatusStatelessTransformer()); + return $fractal->createData($resource)->toArray(); + } - public static function del($id, $purge = false) - { - if($purge) { - $status = self::get($id); - if($status && isset($status['account']) && isset($status['account']['id'])) { - Cache::forget('profile:embed:' . $status['account']['id']); - } - Cache::forget('status:transformer:media:attachments:' . $id); - MediaService::del($id); - Cache::forget('status:thumb:nsfw0' . $id); - Cache::forget('status:thumb:nsfw1' . $id); - Cache::forget('pf:services:sh:id:' . $id); - PublicTimelineService::rem($id); - NetworkTimelineService::rem($id); - } + public static function del($id, $purge = false) + { + if($purge) { + $status = self::get($id); + if($status && isset($status['account']) && isset($status['account']['id'])) { + Cache::forget('profile:embed:' . $status['account']['id']); + } + Cache::forget('status:transformer:media:attachments:' . $id); + MediaService::del($id); + Cache::forget('status:thumb:nsfw0' . $id); + Cache::forget('status:thumb:nsfw1' . $id); + Cache::forget('pf:services:sh:id:' . $id); + PublicTimelineService::rem($id); + NetworkTimelineService::rem($id); + } - Cache::forget(self::key($id, false)); - return Cache::forget(self::key($id)); - } + Cache::forget(self::key($id, false)); + return Cache::forget(self::key($id)); + } - public static function refresh($id) - { - Cache::forget(self::key($id, false)); - Cache::forget(self::key($id, true)); - self::get($id, false); - self::get($id, true); - } + public static function refresh($id) + { + Cache::forget(self::key($id, false)); + Cache::forget(self::key($id, true)); + self::get($id, false); + self::get($id, true); + } - public static function isShared($id, $pid = null) - { - return $pid ? - ReblogService::get($pid, $id) : - false; - } + public static function isShared($id, $pid = null) + { + return $pid ? + ReblogService::get($pid, $id) : + false; + } - public static function isBookmarked($id, $pid = null) - { - return $pid ? - BookmarkService::get($pid, $id) : - false; - } + public static function isBookmarked($id, $pid = null) + { + return $pid ? + BookmarkService::get($pid, $id) : + false; + } } From 6c3699508383710ca04da5d648bec1f8020ba8fa Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 22 Jul 2023 03:50:43 -0600 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bcf22cf0..c1e267a8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ - Update admin user view, improve previews ([ff2c16fe](https://github.com/pixelfed/pixelfed/commit/ff2c16fe)) - Update FanoutDeletePipeline, fix AP object ([0d802c31](https://github.com/pixelfed/pixelfed/commit/0d802c31)) - Update Remote Auth feature, fix custom domain bug and enforce banned domains ([acabf603](https://github.com/pixelfed/pixelfed/commit/acabf603)) +- Update StatusService, reduce cache ttl from 7 days to 6 hours ([59b64378](https://github.com/pixelfed/pixelfed/commit/59b64378)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.11.8 (2023-05-29)](https://github.com/pixelfed/pixelfed/compare/v0.11.7...v0.11.8)