forked from mongofs/scache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpl.go
More file actions
215 lines (187 loc) · 4.89 KB
/
Copy pathimpl.go
File metadata and controls
215 lines (187 loc) · 4.89 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package Scache
import (
"container/list"
"errors"
"sync"
"time"
)
var (
ErrValueIsBiggerThanMaxByte = errors.New("sCache : value size is bigger than maxBytes ")
ErrInValidParam = errors.New("sCache : the param is invalid")
ErrNotifyNotExist = errors.New("sCache : notify is not exist")
ErrTopicAlreadyExist = errors.New("sCache : Topic already exist")
ErrBadConvertParamToCall = errors.New("sCache : Can't Convert Param item[0] to Call")
)
type cacheImpl struct {
rw sync.RWMutex
maxBytes int64
nBytes int64
ll *list.List
cache map[string]*list.Element
OnCaller func(key string, v Value)
locker *Locker
}
func New(maxByte int64, OnCaller func(key string, value Value)) *cacheImpl {
return &cacheImpl{
maxBytes: maxByte,
nBytes: 0,
ll: list.New(),
cache: make(map[string]*list.Element),
OnCaller: OnCaller,
}
}
func (c *cacheImpl) Get(key string) (Value, bool) {
return c.get(key)
}
func (c *cacheImpl) Set(key string, value Value) error {
if value == nil || key == "" {
return ErrInValidParam
}
return c.set(key, value)
}
func (c *cacheImpl) Del(key string) {
c.del(key,false)
}
func (c *cacheImpl) Expire(key string, ttl int64) {
c.expire(key, ttl)
}
func (c *cacheImpl) SetWithTTL(key string, content []byte, ttl int64) error {
panic("implement me")
}
// 一个自带分布式锁的内容
func (c *cacheImpl) GetTargetKeyLockerWithTimeOut(targetKey string, f func() (Value, error)) (Value, error) {
return c.locker.Get(targetKey,f)
}
// =============================================concurrency safe =========================================
// get 并发安全
func (c *cacheImpl) get(key string) (Value, bool) {
c.rw.RLock()
defer c.rw.RUnlock()
if ele, ok := c.getElem(key); ok {
if c.flushKey(ele) {
c.ll.MoveToFront(ele)
kv := ele.Value.(*sds)
return kv.Value, true
} else {
return nil, false
}
}
return nil, false
}
// set 并发安全 ,设置一个值,需要考虑值存在的时候更新和值不存在的时候
// 添加
func (c *cacheImpl) set(key string, value Value) (err error) {
c.rw.Lock()
defer c.rw.Unlock()
if int64(value.Len()) > c.maxBytes {
return ErrValueIsBiggerThanMaxByte
}
if ele, ok := c.getElem(key); ok {
kv := ele.Value.(*sds)
// 可能key存在,但是被标记为已删除,此时只需要从新覆盖这个值
if kv.Status() == SDSStatusDelete {
kv.ReUse()
kv.expire = 0
kv.Value = value
c.nBytes += int64(kv.Calculation())
} else {
oldV := kv.Value
kv.Value = value
// todo 重新设置后将过期时间置零,有待考证
kv.expire = 0
c.nBytes += int64(oldV.Len() - value.Len())
}
} else {
// 创建新的sds结构体
newSds := NewSDS(key, value)
ele := c.ll.PushFront(newSds)
c.cache[key] = ele
c.nBytes += int64(newSds.Calculation())
}
for c.maxBytes != 0 && c.maxBytes < c.nBytes {
c.removeOldest()
}
return
}
func (c *cacheImpl) del(key string, del bool) {
c.rw.Lock()
defer c.rw.Unlock()
if del {
c.RealDel()
return
}
c.fakeDel(key)
}
// =============================================concurrency not safe =========================================
// removeOldest 移除最老的内容
func (c *cacheImpl) removeOldest() {
ele := c.ll.Back()
if ele != nil {
c.ll.Remove(ele)
kv := ele.Value.(*sds)
delete(c.cache, kv.key)
c.nBytes -= int64(len(kv.key)) + int64(kv.Value.Len())
if c.OnCaller != nil {
c.OnCaller(kv.key, kv.Value)
}
}
}
// getElem 并发不安全,需要加锁操作
func (c *cacheImpl) getElem(key string) (*list.Element, bool) {
if ele, ok := c.cache[key]; ok {
return ele, true
}
return nil, false
}
// fakeDel 假删除,将内容标记为删除
func (c *cacheImpl) fakeDel(key string) {
if ele, ok := c.getElem(key); ok {
v := ele.Value.(*sds)
v.Delete()
c.ll.Remove(ele)
c.nBytes -= int64(v.Value.Len())
}
return
}
// realDel For testing ,请勿直接调用 真删除,将标记出来的内容删除, 这是一个On操作,需要在后台线程上进行操作
// 这个并非暴露的接口
func (c *cacheImpl) RealDel() {
for k, v := range c.cache {
tev := v
st := tev.Value.(*sds).Status()
if st == SDSStatusDelete {
delete(c.cache, k)
}
}
}
// flushKey 更新key的状态,如果状态是ok的
func (c *cacheImpl) flushKey(ele *list.Element) bool {
s := ele.Value.(*sds)
// 这个key 标记为被删除
if s.Status() == SDSStatusDelete {
return false
}
// 将这个key 标记为删除,后台线程去进行删除
if s.expire != 0 && s.expire > time.Now().Unix() {
s.Delete()
c.ll.Remove(ele)
return false
}
return true
}
func (c *cacheImpl) expire(key string, ttl int64) {
if ttl <= 0 {
return
}
if v, ok := c.getElem(key); ok {
v.Value.(*sds).expire = time.Now().Unix() + ttl
}
}
func (c *cacheImpl) setNX(key string, value Value) bool {
if _, ok := c.getElem(key); ok {
return false
} else {
c.set(key, value)
}
return true
}