forked from mlogclub/bbs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_cache.go
More file actions
94 lines (83 loc) · 2.13 KB
/
Copy pathuser_cache.go
File metadata and controls
94 lines (83 loc) · 2.13 KB
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
package cache
import (
"errors"
"github.com/mlogclub/simple/date"
"time"
"bbs-go/model"
"bbs-go/repositories"
"github.com/goburrow/cache"
"github.com/mlogclub/simple"
)
type userCache struct {
cache cache.LoadingCache
scoreRankCache cache.LoadingCache
checkInRankCache cache.LoadingCache
}
var UserCache = newUserCache()
func newUserCache() *userCache {
return &userCache{
cache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
value = repositories.UserRepository.Get(simple.DB(), key2Int64(key))
if value == nil {
e = errors.New("数据不存在")
}
return
},
cache.WithMaximumSize(1000),
cache.WithExpireAfterAccess(30*time.Minute),
),
scoreRankCache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
value = repositories.UserRepository.Find(simple.DB(), simple.NewSqlCnd().Desc("score").Limit(10))
if value == nil {
e = errors.New("数据不存在")
}
return
},
cache.WithMaximumSize(10),
cache.WithRefreshAfterWrite(10*time.Minute),
),
checkInRankCache: cache.NewLoadingCache(
func(key cache.Key) (value cache.Value, e error) {
today := date.GetDay(time.Now())
value = repositories.CheckInRepository.Find(simple.DB(),
simple.NewSqlCnd().Eq("latest_day_name", today).Asc("update_time").Limit(10))
return
},
cache.WithMaximumSize(10),
cache.WithExpireAfterAccess(1*time.Hour),
),
}
}
func (c *userCache) Get(userId int64) *model.User {
if userId <= 0 {
return nil
}
val, err := c.cache.Get(userId)
if err != nil {
return nil
}
return val.(*model.User)
}
func (c *userCache) Invalidate(userId int64) {
c.cache.Invalidate(userId)
}
func (c *userCache) GetScoreRank() []model.User {
val, err := c.scoreRankCache.Get("data")
if err != nil {
return nil
}
return val.([]model.User)
}
func (c *userCache) GetCheckInRank() []model.CheckIn {
today := date.GetDay(time.Now())
val, err := c.checkInRankCache.Get(today)
if err != nil {
return nil
}
return val.([]model.CheckIn)
}
func (c *userCache) RefreshCheckInRank() {
c.checkInRankCache.Refresh(date.GetDay(time.Now()))
}