-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocache.go
229 lines (179 loc) · 3.75 KB
/
gocache.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
package gocache
import (
"fmt"
"log"
"runtime"
"sync"
"time"
)
// constants for default cache life
const (
Eternal = -1
)
// ***** Models ****
// cached file content
type Item struct {
Content interface{}
Life int64
}
// garbage collector
type garbageCollector struct {
interval time.Duration
stop chan bool
}
// Cache properties
type Cache struct {
DefaultLife time.Duration
pool *workerPool
mutex sync.RWMutex
items map[string]Item
garbageCollector *garbageCollector
}
//***** Garbage Collector Functions *****
// Garbage Collector
func (j *garbageCollector) Run(c *Cache) {
ticker := time.NewTicker(j.interval)
for {
select {
case <-ticker.C:
c.DeleteExpired()
case <-j.stop:
ticker.Stop()
return
}
}
}
// Stop garbage collector
func stopGC(c *Cache) {
c.garbageCollector.stop <- true
}
func runGC(c *Cache, ci time.Duration) {
gc := &garbageCollector{
interval: ci,
stop: make(chan bool),
}
c.garbageCollector = gc
go gc.Run(c)
}
func (c *Cache) DeleteBackground(key string) {
if c.pool == nil {
log.Println("Pool is empty")
return
}
c.pool.Submit(&deleteJob{job: job{cache: c, key: key}})
}
func (c *Cache) DeleteCachedItem(key string) {
c.mutex.Lock()
delete(c.items, key)
c.mutex.Unlock()
}
func (c *Cache) ClearCache() {
c.mutex.Lock()
for key := range c.items {
delete(c.items, key)
}
c.mutex.Unlock()
}
func (c *Cache) DeleteExpired() {
now := time.Now().UnixNano()
c.mutex.Lock()
for key, item := range c.items {
// Delete items excepts eternal one(v.Life=-1)
if item.Life > 0 && now > item.Life {
delete(c.items, key)
}
}
c.mutex.Unlock()
}
//***** Cache Functions *****
func (c *Cache) set(key string, content interface{}, life time.Duration) {
var lifeTime int64
if life > 0 {
lifeTime = time.Now().Add(life).UnixNano()
} else {
lifeTime = Eternal
}
c.items[key] = Item{
Content: content,
Life: lifeTime,
}
}
func (c *Cache) Set(key string, content interface{}, life time.Duration) {
var lifeTime int64
if life > 0 {
lifeTime = time.Now().Add(life).UnixNano()
} else {
lifeTime = Eternal
}
c.mutex.Lock()
c.items[key] = Item{
Content: content,
Life: lifeTime,
}
c.mutex.Unlock()
}
func (c *Cache) get(key string) (interface{}, bool) {
item, found := c.items[key]
if !found {
return nil, false
}
return item.Content, true
}
func (c *Cache) Get(key string) (interface{}, bool) {
c.mutex.RLock()
item, found := c.items[key]
if !found {
c.mutex.RUnlock()
return nil, false
}
c.mutex.RUnlock()
return item.Content, true
}
func (c *Cache) Add(key string, content interface{}, life time.Duration) (bool, string) {
c.mutex.Lock()
_, found := c.get(key)
if found {
c.mutex.Unlock()
return false, fmt.Sprintf("Item with key: %s already exists", key)
}
c.set(key, content, life)
c.mutex.Unlock()
return true, "OK"
}
func (c *Cache) Print() {
c.mutex.Lock()
defer c.mutex.Unlock()
fmt.Printf("Length: %d\n", len(c.items))
for k, value := range c.items {
fmt.Printf("key: %s value: %v\n", k, value)
}
}
func (c *Cache) AddBackground(key string, content interface{}, life time.Duration) {
if c.pool == nil {
log.Println("Pool is empty")
return
}
c.pool.Submit(&addingJob{job: job{cache: c, key: key, content: content, life: life}})
}
func (c *Cache) StartWorkerPoolWith(workerCount int) {
if c.pool != nil {
return
}
c.pool = newWorkerPool(workerCount)
c.pool.Start()
}
func (c *Cache) StopWorkerPool() {
c.pool.Stop()
}
func NewCache(defaultLife, gbcInterval time.Duration) *Cache {
items := make(map[string]Item)
c := &Cache{
DefaultLife: defaultLife,
items: items,
}
if gbcInterval > 0 {
runGC(c, gbcInterval)
runtime.SetFinalizer(c, stopGC)
}
return c
}