-
Notifications
You must be signed in to change notification settings - Fork 1
/
localcache.go
168 lines (149 loc) · 4.06 KB
/
localcache.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package go_fast_cache
import (
locallog "github.com/daqnext/LocalLog/log"
"github.com/daqnext/go-fast-cache/sortedset"
"github.com/daqnext/go-fast-cache/ttltype"
"github.com/daqnext/go-smart-routine/sr"
"math/rand"
"sync"
"time"
)
const (
MaxTTLSecond = 7200
DefaultCountLimit = 1000000
MinCountLimit = 10000
MaxDeleteExpireIntervalSecond = 300
DefaultDeleteExpireIntervalSecond = 5
DefaultDeleteOverLimitRate = 0.15
)
type LocalCache struct {
s *sortedset.SortedSet
countLimit int64
lock sync.Mutex
llog *locallog.LocalLog
}
// New Instance of localCache, the interval of scheduleDeleteExpire job use the default value 5 seconds
func New(logger *locallog.LocalLog) *LocalCache {
rand.Seed(time.Now().UnixNano())
cache := &LocalCache{
s: sortedset.Make(),
countLimit: DefaultCountLimit,
llog: logger,
}
cache.scheduleDeleteExpire(5)
cache.scheduleDeleteOverLimit()
return cache
}
// NewWithInterval Instance of localCache, param intervalSecond defines the interval of scheduleDeleteExpire job, if intervalSecond <=0,it will use the default value 5 seconds
func NewWithInterval(intervalSecond int, logger *locallog.LocalLog) *LocalCache {
if intervalSecond > MaxDeleteExpireIntervalSecond {
intervalSecond = MaxDeleteExpireIntervalSecond
}
if intervalSecond < 1 {
intervalSecond = DefaultDeleteExpireIntervalSecond
}
cache := &LocalCache{
s: sortedset.Make(),
countLimit: DefaultCountLimit,
llog: logger,
}
cache.scheduleDeleteExpire(intervalSecond)
cache.scheduleDeleteOverLimit()
return cache
}
// SetCountLimit Key count limit,default is 1000000. The 15% of the keys with the most recent expiration time will be deleted if the number of keys exceeds the limit.
func (lc *LocalCache) SetCountLimit(limit int64) {
if limit < MinCountLimit {
limit = MinCountLimit
}
lc.countLimit = limit
}
func (lc *LocalCache) Get(key string) (value interface{}, ttl int64, exist bool) {
//check expire
e, exist := lc.s.Get(key)
if !exist {
return nil, 0, false
}
nowTime := time.Now().Unix()
if e.Score <= nowTime {
return nil, 0, false
}
return e.Value, e.Score - nowTime, true
}
// Set Set key value with expire time, ttl.Keep or second. If key not exist and set ttl ttl.Keep,it will use default ttl 30sec
func (lc *LocalCache) Set(key string, value interface{}, ttlSecond int64) {
if ttlSecond < 0 {
return
}
if ttlSecond > MaxTTLSecond {
ttlSecond = MaxTTLSecond
}
var expireTime int64
if ttlSecond == ttltype.Keep {
//keep
ttlLeft, exist := lc.ttl(key)
if !exist {
ttlLeft = 30
}
expireTime = time.Now().Unix() + ttlLeft
} else {
//new expire
expireTime = time.Now().Unix() + ttlSecond
}
lc.s.Add(key, expireTime, value)
}
func (lc *LocalCache) Delete(key string) {
lc.s.Remove(key)
}
// TTL get ttl of a key with second
func (lc *LocalCache) ttl(key string) (int64, bool) {
e, exist := lc.s.Get(key)
if !exist {
return 0, false
}
ttl := e.Score - time.Now().Unix()
if ttl <= 0 {
return 0, false
}
return ttl, true
}
func (lc *LocalCache) scheduleDeleteOverLimit() {
sr.New_Panic_Redo(func() {
time.Sleep(500 * time.Millisecond)
for {
time.Sleep(1 * time.Second)
//log.Println("scheduleDeleteOverLimit start")
if lc.s.Len() >= lc.countLimit {
deleteCount := float64(lc.countLimit) * DefaultDeleteOverLimitRate
lc.s.RemoveByRank(0, int64(deleteCount))
}
}
}, lc.llog).Start()
}
// ScheduleDeleteExpire delete expired keys
func (lc *LocalCache) scheduleDeleteExpire(intervalSecond int) {
sr.New_Panic_Redo(func() {
for {
time.Sleep(time.Duration(intervalSecond) * time.Second)
//log.Println("scheduleDeleteExpire start")
max := time.Now().Unix()
//remove expired keys
lc.s.RemoveByScore(max)
}
}, lc.llog).Start()
}
func (lc *LocalCache) GetLen() int64 {
return lc.s.Len()
}
func (lc *LocalCache) SetRand(key string, ttlSecond int64) string {
rs := genRandStr(20)
lc.Set(key, rs, ttlSecond)
return rs
}
func (lc *LocalCache) GetRand(key string) string {
v, _, exist := lc.Get(key)
if !exist {
return ""
}
return v.(string)
}