Skip to content

Commit 6d98164

Browse files
authored
Update Trending.php
1 parent fdd34fb commit 6d98164

File tree

1 file changed

+6
-25
lines changed

1 file changed

+6
-25
lines changed

app/Trending.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App;
44

5-
use Illuminate\Support\Facades\Cache;
5+
use Illuminate\Support\Facades\Redis;
66

77
class 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

Comments
 (0)