-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
251 lines (217 loc) · 5.64 KB
/
cache.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package memory_cache
import (
"memory_cache/list"
"sync"
"time"
)
//--------------------------------------
// item views
//--------------------------------------
type ItemViews struct {
key string
view int
lastViewTime time.Time
}
func NewItemViews(key string) *ItemViews {
return &ItemViews{
key: key,
view: 1,
lastViewTime: time.Now(),
}
}
func (this *ItemViews) Destory(i interface{}) {}
func (this *ItemViews) Equal(i1 interface{}, i2 interface{}) bool {
if _, ok := i1.(*ItemViews); !ok {
return false
}
if _, ok := i2.(*ItemViews); !ok {
return false
}
return i1.(*ItemViews).key == i2.(*ItemViews).key
}
func (this *ItemViews) UpdateWithExpireExam(d time.Duration) (view int, expired bool) {
if expired = this.expired(d); expired {
view = this.view
return
}
this.view++
view = this.view
this.lastViewTime = time.Now()
return
}
func (this *ItemViews) expired(d time.Duration) bool {
if time.Now().Sub(this.lastViewTime) > d {
this.view = 1
return true
}
return false
}
func (this *ItemViews) Reset() *ItemViews {
this.view = 1
this.lastViewTime = time.Now()
return this
}
//--------------------------------------
// cache item
//--------------------------------------
type CacheItem struct {
data interface{}
views *ItemViews
}
func (this *CacheItem) Destory(i interface{}) {}
func (this *CacheItem) Equal(i1 interface{}, i2 interface{}) bool {
if _, ok := i1.(*CacheItem); !ok {
return false
}
if _, ok := i2.(*CacheItem); !ok {
return false
}
return i1.(*CacheItem).views.key == i2.(*CacheItem).views.key
}
func NewCacheItem(views *ItemViews, d interface{}) *CacheItem {
return &CacheItem{
data: d,
views: views,
}
}
//--------------------------------------
// view history
//--------------------------------------
type ViewHistory struct {
k int
size int
items *list.List
expire time.Duration
}
func NewViewHistory(size int, expire time.Duration, k int) *ViewHistory {
return &ViewHistory{
k: k,
size: size,
items: list.NewList(&ItemViews{}),
expire: expire,
}
}
func (this *ViewHistory) Put(key string) (new *list.Element, old *list.Element) {
view := NewItemViews(key)
new = this.items.Insert(nil, view)
if this.items.Size > this.size {
old = this.items.Tail
this.items.Delete(this.items.Tail)
}
return
}
func (this *ViewHistory) UpdateView(pointer *list.Element) (cached bool) {
this.items.Delete(pointer)
if views, _ := pointer.Data.(*ItemViews).UpdateWithExpireExam(this.expire); views > this.k {
cached = true
return
}
this.items.Insert(nil, pointer.Data)
return
}
//--------------------------------------
// memory cache
//--------------------------------------
type MemoryCache struct {
size int
items *list.List
expire time.Duration
}
func NewMemoryCache(size int, expire time.Duration) *MemoryCache {
return &MemoryCache{
size: size,
expire: expire,
items: list.NewList(&CacheItem{}),
}
}
func (this *MemoryCache) Put(i *ItemViews, data interface{}) (new *list.Element, old *list.Element) {
new = this.items.Insert(nil, NewCacheItem(i.Reset(), data))
if this.items.Size > this.size {
old = this.items.Tail
this.items.Delete(this.items.Tail)
}
return
}
func (this *MemoryCache) UpdateData(pointer *list.Element, data interface{}) {
pointer.Data.(*CacheItem).data = data
this.items.Delete(pointer)
this.items.Insert(nil, pointer.Data)
}
func (this *MemoryCache) UpdateView(pointer *list.Element) (expired bool) {
this.items.Delete(pointer)
if _, expired = pointer.Data.(*CacheItem).views.UpdateWithExpireExam(this.expire); expired {
return
}
this.items.Insert(nil, pointer.Data)
return
}
//--------------------------------------
// LRU-K memory cache manager
//--------------------------------------
type MemoryCacheManager struct {
history *ViewHistory
cache *MemoryCache
historyMap *sync.Map
cacheMap *sync.Map
historyLock *sync.Mutex
cacheLock *sync.Mutex
}
func NewMemoryCacheManager(historySize int, hisotrySatisfyK int, historyExpire time.Duration, memoryCacheSize int, memoryCacheExpire time.Duration) *MemoryCacheManager {
return &MemoryCacheManager{
history: NewViewHistory(historySize, historyExpire, hisotrySatisfyK),
cache: NewMemoryCache(memoryCacheSize, memoryCacheExpire),
historyMap: &sync.Map{},
cacheMap: &sync.Map{},
historyLock: &sync.Mutex{},
cacheLock: &sync.Mutex{},
}
}
func (this *MemoryCacheManager) Get(key string) (data interface{}) {
this.historyLock.Lock()
defer this.historyLock.Unlock()
if val, ok := this.historyMap.Load(key); ok {
if cached := this.history.UpdateView(val.(*list.Element)); cached {
n, old := this.cache.Put(val.(*list.Element).Data.(*ItemViews), nil)
this.historyMap.Delete(key)
this.cacheMap.Store(key, n)
if old != nil {
this.cacheMap.Delete(old.Data.(*CacheItem).views.key)
}
}
} else if val, ok = this.cacheMap.Load(key); ok {
expire := time.Now().Add(500 * time.Millisecond)
for {
if time.Now().After(expire) {
break
}
if data = val.(*list.Element).Data.(*CacheItem).data; data != nil {
break
}
}
this.cacheLock.Lock()
defer this.cacheLock.Unlock()
if this.cache.UpdateView(val.(*list.Element)) {
this.cacheMap.Delete(key)
return
}
} else {
n, old := this.history.Put(key)
this.historyMap.Store(key, n)
if old != nil {
this.historyMap.Delete(old.Data.(*ItemViews).key)
}
}
return
}
func (this *MemoryCacheManager) Put(key string, data interface{}) {
this.cacheLock.Lock()
defer this.cacheLock.Unlock()
var (
ok bool
val interface{}
)
if val, ok = this.cacheMap.Load(key); !ok {
return
}
this.cache.UpdateData(val.(*list.Element), data)
}