-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
157 lines (132 loc) · 3.25 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
package pxy
import (
"time"
"sync"
"container/list"
)
const Deleting = "deleting"
const Promoting = "promoting"
type LruOperation struct {
action string
item *CacheItem
}
type Cache struct {
sync.RWMutex
list *list.List
size uint64
maxSize uint64
sizeToPrung uint64
items map[string]*CacheItem
lruables chan *LruOperation
}
type CacheItem struct {
key string
Data []byte
Expiration int64
element *list.Element
deleted bool
}
func NewCache(maxSize, sizeToPrung uint64) *Cache {
cache := &Cache {
list: list.New(),
size: 0,
sizeToPrung: sizeToPrung,
maxSize: maxSize,
items: make(map[string]*CacheItem),
lruables: make(chan *LruOperation, 10),
}
go cache.worker()
return cache
}
func (cache *Cache) Delete(key string) *CacheItem {
cache.Lock()
item := cache.items[key]
delete(cache.items, key)
cache.Unlock()
if (item != nil) {
cache.deleteable(item)
}
return item
}
func (cache *Cache) Get(key string) (cacheItem *CacheItem, ok bool) {
cache.RLock()
cacheItem = cache.items[key]
cache.RUnlock()
if cacheItem == nil {
ok = false
cacheItem = nil
return
}
if time.Now().Unix() > cacheItem.Expiration {
ok = false
cacheItem.Data = nil
return
}
cache.promotable(cacheItem)
ok = true
return
}
func (cache *Cache) deleteable(item *CacheItem) {
cache.lruables <- &LruOperation {
action: Deleting,
item: item,
}
}
func (cache *Cache) promotable(item *CacheItem) {
cache.lruables <- &LruOperation {
action: Promoting,
item: item,
}
}
func (cache *Cache) Set(key string, cacheItem *CacheItem) {
cache.Lock()
existing := cache.items[key]
cache.items[key] = cacheItem
cache.Unlock()
if (existing != nil) {
cache.deleteable(existing)
}
cache.promotable(cacheItem)
}
func (cache *Cache) promoteItem(cacheItem *CacheItem) bool {
cache.size += uint64(len(cacheItem.Data))
if (cacheItem.element != nil) {
cache.list.MoveToFront(cacheItem.element)
return true
}
cacheItem.element = cache.list.PushFront(cacheItem)
return true
}
func (cache *Cache) removeFromList(item *CacheItem) {
cache.size -= uint64(len(item.Data))
cache.list.Remove(item.element)
}
func (cache *Cache) worker() {
for {
operation := <-cache.lruables
item := operation.item
switch operation.action {
case Deleting:
cache.removeFromList(item)
case Promoting:
if cache.promoteItem(item) && cache.size > cache.maxSize {
cache.gc()
}
}
}
}
func (cache *Cache) gc() {
currentSize := cache.size
aimToPrune := cache.maxSize - cache.sizeToPrung
element := cache.list.Back()
for currentSize > aimToPrune {
item := element.Value.(*CacheItem)
prevElement := element.Prev()
cache.Lock()
delete(cache.items, item.key)
cache.Unlock()
cache.removeFromList(item)
element = prevElement
currentSize -= uint64(len(item.Data))
}
}