-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
145 lines (114 loc) · 3.41 KB
/
view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris polaris@studygolang.com
package logic
import (
"fmt"
"github.com/ue4-community/learnue/modules/setting"
"net/http"
"strconv"
"strings"
"sync"
. "github.com/ue4-community/learnue/db"
"github.com/ue4-community/learnue/model"
"github.com/ue4-community/learnue/modules/goutils"
"github.com/ue4-community/learnue/modules/logger"
)
// 话题/文章/资源/图书等的浏览数
// 避免每次写库,同时避免刷屏
type view struct {
objtype int // 对象类型(model/comment 中的 type 常量)
objid int // 对象id(相应的表中的id)
num int // 当前浏览数
locker sync.Mutex
}
func newView(objtype, objid int) *view {
return &view{objtype: objtype, objid: objid}
}
func (this *view) incr() {
this.locker.Lock()
defer this.locker.Unlock()
this.num++
}
// flush 将浏览数刷入数据库中
func (this *view) flush() {
this.locker.Lock()
defer this.locker.Unlock()
session := MasterDB.Id(this.objid)
switch this.objtype {
case model.TypeTopic:
session.Incr("view", this.num).Update(new(model.TopicUpEx))
case model.TypeArticle:
session.Incr("viewnum", this.num).Update(new(model.Article))
case model.TypeResource:
session.Incr("viewnum", this.num).Update(new(model.ResourceEx))
case model.TypeProject:
session.Incr("viewnum", this.num).Update(new(model.OpenProject))
case model.TypeWiki:
session.Incr("viewnum", this.num).Update(new(model.Wiki))
case model.TypeBook:
session.Incr("viewnum", this.num).Update(new(model.Book))
}
DefaultRank.GenDayRank(this.objtype, this.objid, this.num)
this.num = 0
}
// 保存所有对象的浏览数
type views struct {
data map[string]*view
// 记录用户是否已经看过(防止刷屏)
users map[string]bool
locker sync.Mutex
}
func newViews() *views {
return &views{data: make(map[string]*view), users: make(map[string]bool)}
}
// TODO: 用户登录了,应该用用户标识,而不是IP
func (this *views) Incr(req *http.Request, objtype, objid int, uids ...int) {
ua := req.UserAgent()
spiders := strings.Split(setting.Get().GetString("global.spider"), ",")
for _, spider := range spiders {
if strings.Contains(ua, spider) {
return
}
}
// 记录浏览来源
go DefaultViewSource.Record(req, objtype, objid)
key := strconv.Itoa(objtype) + strconv.Itoa(objid)
var userKey string
if len(uids) > 0 {
userKey = fmt.Sprintf("%s_uid_%d", key, uids[0])
} else {
userKey = fmt.Sprintf("%s_ip_%d", key, goutils.Ip2long(goutils.RemoteIp(req)))
}
this.locker.Lock()
defer this.locker.Unlock()
if _, ok := this.users[userKey]; ok {
return
} else {
this.users[userKey] = true
}
if _, ok := this.data[key]; !ok {
this.data[key] = newView(objtype, objid)
}
this.data[key].incr()
if len(uids) > 0 {
ViewObservable.NotifyObservers(uids[0], objtype, objid)
} else {
ViewObservable.NotifyObservers(0, objtype, objid)
}
}
func (this *views) Flush() {
logger.Debugln("start views flush")
this.locker.Lock()
defer this.locker.Unlock()
// TODO:量大时,考虑copy一份,然后异步 入库,以免堵塞 锁 太久
for _, view := range this.data {
view.flush()
}
this.data = make(map[string]*view)
this.users = make(map[string]bool)
logger.Debugln("end views flush")
}
var Views = newViews()