From 468a4203ca223fba771688cafac247d41d6b210b Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 30 Jun 2019 22:27:27 -0600 Subject: [PATCH] Add StatusHashtagService --- app/Services/StatusHashtagService.php | 73 +++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 app/Services/StatusHashtagService.php diff --git a/app/Services/StatusHashtagService.php b/app/Services/StatusHashtagService.php new file mode 100644 index 000000000..0c5120726 --- /dev/null +++ b/app/Services/StatusHashtagService.php @@ -0,0 +1,73 @@ + 2000 ? 2000 : $stop; + $ids = Redis::zrangebyscore($key, $start, $stop); + if(empty($ids)) { + $ids = self::coldGet($id, $start, $stop); + } + foreach($ids as $statusId) { + $res->push(self::getStatus($statusId, $id)); + } + return $res; + } + + public static function coldGet($id, $start = 0, $stop = 2000) + { + $stop = $stop > 2000 ? 2000 : $stop; + $ids = StatusHashtag::whereHashtagId($id) + ->latest() + ->skip($start) + ->take($stop) + ->pluck('status_id'); + foreach($ids as $key) { + self::set($id, $key); + } + return $ids; + } + + public static function set($key, $val) + { + return Redis::zadd(self::CACHE_KEY . $key, $val, $val); + } + + public static function del($key) + { + return Redis::zrem(self::CACHE_KEY . $key, $val); + } + + public static function count($id) + { + return Redis::zcount(self::CACHE_KEY . $id, '-inf', '+inf'); + } + + public static function getStatus($statusId, $hashtagId) + { + return Cache::remember('pf:services:status-hashtag:post:'.$statusId.':hashtag:'.$hashtagId, now()->addMonths(3), function() use($statusId, $hashtagId) { + $statusHashtag = StatusHashtag::with('profile', 'status', 'hashtag') + ->whereStatusId($statusId) + ->whereHashtagId($hashtagId) + ->first(); + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Item($statusHashtag, new StatusHashtagTransformer()); + return $fractal->createData($resource)->toArray(); + }); + } +} \ No newline at end of file