22
33namespace App ;
44
5- use Illuminate \Support \Facades \Cache ;
5+ use Illuminate \Support \Facades \Redis ;
66
77class Trending
88{
@@ -13,10 +13,7 @@ class Trending
1313 */
1414 public function get ()
1515 {
16- return Cache::get ($ this ->cacheKey (), collect ())
17- ->sortByDesc ('score ' )
18- ->slice (0 , 5 )
19- ->values ();
16+ return array_map ('json_decode ' , Redis::zrevrange ($ this ->cacheKey (), 0 , 9 ));
2017 }
2118
2219 /**
@@ -34,35 +31,19 @@ private function cacheKey()
3431 *
3532 * @param Thread $thread
3633 */
37- public function push ($ thread, $ increment = 1 )
34+ public function push ($ thread )
3835 {
39- $ trending = Cache::get ($ this ->cacheKey (), collect ());
40-
41- $ trending [$ thread ->id ] = (object ) [
42- 'score ' => $ this ->score ($ thread ) + $ increment ,
36+ Redis::zincrby ($ this ->cacheKey (), 1 , json_encode ([
4337 'title ' => $ thread ->title ,
4438 'path ' => $ thread ->path (),
45- ];
46-
47- Cache::forever ($ this ->cacheKey (), $ trending );
48- }
49-
50- public function score ($ thread )
51- {
52- $ trending = Cache::get ($ this ->cacheKey (), collect ());
53-
54- if (!isset ($ trending [$ thread ->id ])) {
55- return 0 ;
56- }
57-
58- return $ trending [$ thread ->id ]->score ;
39+ ]));
5940 }
6041
6142 /**
6243 * 重置所有热门线程.
6344 */
6445 public function reset ()
6546 {
66- return Cache:: forget ($ this ->cacheKey ());
47+ Redis:: del ($ this ->cacheKey ());
6748 }
6849}
0 commit comments