Skip to content

Commit fdd34fb

Browse files
authored
Merge pull request #30 from AmazonPython/master
Updated README.md file.
2 parents b0f6bb6 + 7f8ea4d commit fdd34fb

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# Laravel-Forum [![Badge](https://data.jsdelivr.com/v1/package/gh/AmazonPython/Laravel-Forum/badge)](https://data.jsdelivr.com/v1/package/gh/AmazonPython/Laravel-Forum/badge) [![Badge rank](https://data.jsdelivr.com/v1/package/gh/AmazonPython/Laravel-Forum/badge/rank)](https://data.jsdelivr.com/v1/package/gh/AmazonPython/Laravel-Forum/badge/rank) [![License](https://cdn.jsdelivr.net/gh/AmazonPython/Laravel-Forum@master/public/images/License-Anti-996-green.svg)](https://cdn.jsdelivr.net/gh/AmazonPython/Laravel-Forum@master/public/images/License-Anti-996-green.svg)
1+
# Laravel-Forum
2+
[![Badge](https://data.jsdelivr.com/v1/package/gh/AmazonPython/Laravel-Forum/badge)](https://data.jsdelivr.com/v1/package/gh/AmazonPython/Laravel-Forum/badge)
3+
[![Badge rank](https://data.jsdelivr.com/v1/package/gh/AmazonPython/Laravel-Forum/badge/rank)](https://data.jsdelivr.com/v1/package/gh/AmazonPython/Laravel-Forum/badge/rank)
4+
[![License](https://cdn.jsdelivr.net/gh/AmazonPython/Laravel-Forum@master/public/images/License-Anti-996-green.svg)](https://cdn.jsdelivr.net/gh/AmazonPython/Laravel-Forum@master/public/images/License-Anti-996-green.svg)
5+
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/AmazonPython/Laravel-Forum/Laravel?style=flat-square)](https://github.com/AmazonPython/Laravel-Forum/actions?query=workflow%3ALaravel)
6+
[![StyleCI](https://github.styleci.io/repos/285720340/shield?branch=master)](https://github.styleci.io/repos/285720340?branch=master)
27

38
The forum is built based on Laravel. Compliance with BSD 3-Clause License and anti 996 License.
49

app/Trending.php

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

33
namespace App;
44

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

77
class Trending
88
{
@@ -13,7 +13,10 @@ class Trending
1313
*/
1414
public function get()
1515
{
16-
return array_map('json_decode', Redis::zrevrange($this->cacheKey(), 0, 9));
16+
return Cache::get($this->cacheKey(), collect())
17+
->sortByDesc('score')
18+
->slice(0, 5)
19+
->values();
1720
}
1821

1922
/**
@@ -31,19 +34,35 @@ private function cacheKey()
3134
*
3235
* @param Thread $thread
3336
*/
34-
public function push($thread)
37+
public function push($thread, $increment = 1)
3538
{
36-
Redis::zincrby($this->cacheKey(), 1, json_encode([
39+
$trending = Cache::get($this->cacheKey(), collect());
40+
41+
$trending[$thread->id] = (object) [
42+
'score' => $this->score($thread) + $increment,
3743
'title' => $thread->title,
3844
'path' => $thread->path(),
39-
]));
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;
4059
}
4160

4261
/**
4362
* 重置所有热门线程.
4463
*/
4564
public function reset()
4665
{
47-
Redis::del($this->cacheKey());
66+
return Cache::forget($this->cacheKey());
4867
}
4968
}

0 commit comments

Comments
 (0)