2
2
3
3
namespace App ;
4
4
5
- use Illuminate \Support \Facades \Cache ;
5
+ use Illuminate \Support \Facades \Redis ;
6
6
7
7
class Trending
8
8
{
@@ -13,10 +13,7 @@ class Trending
13
13
*/
14
14
public function get ()
15
15
{
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 ));
20
17
}
21
18
22
19
/**
@@ -34,35 +31,19 @@ private function cacheKey()
34
31
*
35
32
* @param Thread $thread
36
33
*/
37
- public function push ($ thread, $ increment = 1 )
34
+ public function push ($ thread )
38
35
{
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 ([
43
37
'title ' => $ thread ->title ,
44
38
'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
+ ]));
59
40
}
60
41
61
42
/**
62
43
* 重置所有热门线程.
63
44
*/
64
45
public function reset ()
65
46
{
66
- return Cache:: forget ($ this ->cacheKey ());
47
+ Redis:: del ($ this ->cacheKey ());
67
48
}
68
49
}
0 commit comments