浏览代码

Update PublicTimelineService

Daniel Supernault 4 年之前
父节点
当前提交
918016a5ad
共有 2 个文件被更改,包括 34 次插入10 次删除
  1. 32 9
      app/Services/PublicTimelineService.php
  2. 2 1
      app/Services/StatusService.php

+ 32 - 9
app/Services/PublicTimelineService.php

@@ -18,18 +18,41 @@ class PublicTimelineService {
 		if($stop > 100) {
 			$stop = 100;
 		}
-		$tl = [];
-		$keys = Redis::zrevrange(self::CACHE_KEY, $start, $stop);
-		foreach($keys as $key) {
-			array_push($tl, StatusService::get($key));
+
+		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 [];
 		}
-		return $tl;
+
+		return array_keys(Redis::zrevrangebyscore(self::CACHE_KEY, '+inf', $end, [
+			'withscores' => true,
+			'limit' => [0, $limit]
+		]));
 	}
 
 	public static function add($val)
 	{
-		// return Redis::zadd(self::CACHE_KEY, $val, $val);
-		return;
+		if(self::count() > 400) {
+			Redis::zpopmin(self::CACHE_KEY);
+		}
+
+		return Redis::zadd(self::CACHE_KEY, $val, $val);
 	}
 
 	public static function rem($val)
@@ -44,7 +67,7 @@ class PublicTimelineService {
 
 	public static function count()
 	{
-		return Redis::zcount(self::CACHE_KEY, '-inf', '+inf');
+		return Redis::zcard(self::CACHE_KEY);
 	}
 
 	public static function warmCache($force = false, $limit = 100)
@@ -55,7 +78,7 @@ class PublicTimelineService {
 				->whereNull('reblog_of_id')
 				->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
 				->whereScope('public')
-				->latest()
+				->orderByDesc('id')
 				->limit($limit)
 				->pluck('id');
 			foreach($ids as $id) {

+ 2 - 1
app/Services/StatusService.php

@@ -37,6 +37,7 @@ class StatusService {
 
 	public static function del($id)
 	{
+		PublicTimelineService::rem($id);
 		return Cache::forget(self::key($id));
 	}
-}
+}