Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1112d9a

Browse files
committed
阅读排行榜:完成数据计算和存储
1 parent c85827a commit 1112d9a

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

config/env.sample.ini

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ show_sql = true
2727
; 0-debug, 1-info, 2-warning, 3-error, 4-off, 5-unknow
2828
log_level = 0
2929

30+
[redis]
31+
host = 127.0.0.1
32+
port = 6379
33+
password =
34+
; 连接超时
35+
conn_timeout = 2
36+
read_timeout = 2
37+
write_timeout = 2
38+
39+
; 最大空闲连接数
40+
max_idle = 2
41+
42+
prefix = studygolang:
43+
3044
; 用于注册发送激活码等
3145
[email]
3246
smtp_username = [email protected]

src/logic/rank.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2017 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author:polaris [email protected]
6+
7+
package logic
8+
9+
import (
10+
"fmt"
11+
"time"
12+
13+
"github.com/polaris1119/logger"
14+
"github.com/polaris1119/nosql"
15+
"github.com/polaris1119/times"
16+
)
17+
18+
type RankLogic struct{}
19+
20+
var DefaultRank = RankLogic{}
21+
22+
func (self RankLogic) GenDayRank(objtype, objid, num int) {
23+
redisClient := nosql.NewRedisClient()
24+
key := self.getDayRankKey(objtype, times.Format("ymd"))
25+
err := redisClient.ZINCRBY(key, num, objid)
26+
if err != nil {
27+
logger.Errorln("view redis ZINCRBY error:", err)
28+
}
29+
redisClient.EXPIRE(key, 2*30*86400)
30+
}
31+
32+
// GenWeekRank 过去 7 天排行榜
33+
func (self RankLogic) GenWeekRank(objtype int) {
34+
redisClient := nosql.NewRedisClient()
35+
dest := self.getWeekRankKey(objtype)
36+
37+
keys := self.getMultiKey(objtype, 7)
38+
39+
err := redisClient.ZUNIONSTORE(dest, 7, keys, nil)
40+
if err != nil {
41+
logger.Errorln("GenWeekRank ZUNIONSTORE error:", err)
42+
}
43+
}
44+
45+
// GenMonthRank 过去 30 天排行榜
46+
func (self RankLogic) GenMonthRank(objtype int) {
47+
redisClient := nosql.NewRedisClient()
48+
dest := self.getMonthRankKey(objtype)
49+
50+
keys := self.getMultiKey(objtype, 30)
51+
52+
err := redisClient.ZUNIONSTORE(dest, 30, keys, nil)
53+
if err != nil {
54+
logger.Errorln("GenMonthRank ZUNIONSTORE error:", err)
55+
}
56+
}
57+
58+
func (self RankLogic) getMultiKey(objtype, num int) []string {
59+
today := time.Now()
60+
61+
keys := make([]string, num)
62+
for i := 0; i < num; i++ {
63+
ymd := times.Format("ymd", today.Add(time.Duration(-(i+1)*86400)*time.Second))
64+
keys[i] = self.getDayRankKey(objtype, ymd)
65+
}
66+
67+
return keys
68+
}
69+
70+
func (RankLogic) getDayRankKey(objtype int, ymd string) string {
71+
return fmt.Sprintf("view:type-%d:rank:%s", objtype, ymd)
72+
}
73+
74+
func (RankLogic) getWeekRankKey(objtype int) string {
75+
return fmt.Sprintf("view:type-%d:rank:last-week", objtype)
76+
}
77+
78+
func (RankLogic) getMonthRankKey(objtype int) string {
79+
return fmt.Sprintf("view:type-%d:rank:last-month", objtype)
80+
}

src/logic/view.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/polaris1119/logger"
1919
)
2020

21-
// 话题/文章/资源的浏览数
21+
// 话题/文章/资源/图书等的浏览数
2222
// 避免每次写库,同时避免刷屏
2323
type view struct {
2424
objtype int // 对象类型(model/comment 中的 type 常量)
@@ -60,6 +60,8 @@ func (this *view) flush() {
6060
session.Incr("viewnum", this.num).Update(new(model.Book))
6161
}
6262

63+
DefaultRank.GenDayRank(this.objtype, this.objid, this.num)
64+
6365
this.num = 0
6466
}
6567

src/server/studygolang/background.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"db"
1111
"global"
1212
"logic"
13+
"model"
1314
"time"
1415

1516
"github.com/polaris1119/logger"
@@ -34,6 +35,9 @@ func ServeBackGround() {
3435
// 每天对非活跃用户降频
3536
c.AddFunc("@daily", decrUserActiveWeight)
3637

38+
// 生成阅读排行榜
39+
c.AddFunc("@daily", genViewRank)
40+
3741
// 两分钟刷一次浏览数(TODO:重启丢失问题?信号控制重启?)
3842
c.AddFunc("@every 2m", logic.Views.Flush)
3943

@@ -101,3 +105,18 @@ func decrUserActiveWeight() {
101105

102106
logger.Debugln("end decr user active weight...")
103107
}
108+
109+
func genViewRank() {
110+
needRankTypes := []int{
111+
model.TypeTopic,
112+
model.TypeResource,
113+
model.TypeProject,
114+
model.TypeArticle,
115+
model.TypeBook,
116+
}
117+
118+
for _, objtype := range needRankTypes {
119+
logic.DefaultRank.GenWeekRank(objtype)
120+
logic.DefaultRank.GenMonthRank(objtype)
121+
}
122+
}

0 commit comments

Comments
 (0)