From 1f35da0d4bfa78fd0d6287e20ebe06a391d468c7 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 11 Nov 2023 05:25:23 -0700 Subject: [PATCH] Update HashtagServices --- app/Services/HashtagFollowService.php | 21 +++++ app/Services/HashtagService.php | 116 +++++++++++++++----------- 2 files changed, 86 insertions(+), 51 deletions(-) create mode 100644 app/Services/HashtagFollowService.php diff --git a/app/Services/HashtagFollowService.php b/app/Services/HashtagFollowService.php new file mode 100644 index 000000000..8ddc9e6e2 --- /dev/null +++ b/app/Services/HashtagFollowService.php @@ -0,0 +1,21 @@ +pluck('profile_id')->toArray(); + }); + } +} diff --git a/app/Services/HashtagService.php b/app/Services/HashtagService.php index 87f895a65..81a7ae4ed 100644 --- a/app/Services/HashtagService.php +++ b/app/Services/HashtagService.php @@ -8,65 +8,79 @@ use App\Hashtag; use App\StatusHashtag; use App\HashtagFollow; -class HashtagService { +class HashtagService +{ + const FOLLOW_KEY = 'pf:services:hashtag:following:v1:'; + const FOLLOW_PIDS_KEY = 'pf:services:hashtag-follows:v1:'; - const FOLLOW_KEY = 'pf:services:hashtag:following:'; + public static function get($id) + { + return Cache::remember('services:hashtag:by_id:' . $id, 3600, function() use($id) { + $tag = Hashtag::find($id); + if(!$tag) { + return []; + } + return [ + 'name' => $tag->name, + 'slug' => $tag->slug, + ]; + }); + } - public static function get($id) - { - return Cache::remember('services:hashtag:by_id:' . $id, 3600, function() use($id) { - $tag = Hashtag::find($id); - if(!$tag) { - return []; - } - return [ - 'name' => $tag->name, - 'slug' => $tag->slug, - ]; - }); - } + public static function count($id) + { + return Cache::remember('services:hashtag:public-count:by_id:' . $id, 86400, function() use($id) { + return StatusHashtag::whereHashtagId($id)->whereStatusVisibility('public')->count(); + }); + } - public static function count($id) - { - return Cache::remember('services:hashtag:public-count:by_id:' . $id, 86400, function() use($id) { - return StatusHashtag::whereHashtagId($id)->whereStatusVisibility('public')->count(); - }); - } + public static function isFollowing($pid, $hid) + { + $res = Redis::zscore(self::FOLLOW_KEY . $hid, $pid); + if($res) { + return true; + } - public static function isFollowing($pid, $hid) - { - $res = Redis::zscore(self::FOLLOW_KEY . $pid, $hid); - if($res) { - return true; - } + $synced = Cache::get(self::FOLLOW_KEY . 'acct:' . $pid . ':synced'); + if(!$synced) { + $tags = HashtagFollow::whereProfileId($pid) + ->get() + ->each(function($tag) use($pid) { + self::follow($pid, $tag->hashtag_id); + }); + Cache::set(self::FOLLOW_KEY . 'acct:' . $pid . ':synced', true, 1209600); - $synced = Cache::get(self::FOLLOW_KEY . $pid . ':synced'); - if(!$synced) { - $tags = HashtagFollow::whereProfileId($pid) - ->get() - ->each(function($tag) use($pid) { - self::follow($pid, $tag->hashtag_id); - }); - Cache::set(self::FOLLOW_KEY . $pid . ':synced', true, 1209600); + return (bool) Redis::zscore(self::FOLLOW_KEY . $hid, $pid) >= 1; + } - return (bool) Redis::zscore(self::FOLLOW_KEY . $pid, $hid) > 1; - } + return false; + } - return false; - } + public static function follow($pid, $hid) + { + Cache::forget(self::FOLLOW_PIDS_KEY . $hid); + return Redis::zadd(self::FOLLOW_KEY . $hid, $pid, $pid); + } - public static function follow($pid, $hid) - { - return Redis::zadd(self::FOLLOW_KEY . $pid, $hid, $hid); - } + public static function unfollow($pid, $hid) + { + Cache::forget(self::FOLLOW_PIDS_KEY . $hid); + return Redis::zrem(self::FOLLOW_KEY . $hid, $pid); + } - public static function unfollow($pid, $hid) - { - return Redis::zrem(self::FOLLOW_KEY . $pid, $hid); - } + public static function following($hid, $start = 0, $limit = 10) + { + $synced = Cache::get(self::FOLLOW_KEY . 'acct-following:' . $hid . ':synced'); + if(!$synced) { + $tags = HashtagFollow::whereHashtagId($hid) + ->get() + ->each(function($tag) use($hid) { + self::follow($tag->profile_id, $hid); + }); + Cache::set(self::FOLLOW_KEY . 'acct-following:' . $hid . ':synced', true, 1209600); - public static function following($pid, $start = 0, $limit = 10) - { - return Redis::zrevrange(self::FOLLOW_KEY . $pid, $start, $limit); - } + return Redis::zrevrange(self::FOLLOW_KEY . $hid, $start, $limit); + } + return Redis::zrevrange(self::FOLLOW_KEY . $hid, $start, $limit); + } }