forked from bluele/gcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
164 lines (140 loc) · 3.29 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
package gcache
import (
"errors"
"sync"
"time"
)
const (
TYPE_SIMPLE = "simple"
TYPE_LRU = "lru"
TYPE_LFU = "lfu"
TYPE_ARC = "arc"
)
var KeyNotFoundError = errors.New("Key not found.")
type Cache interface {
Set(interface{}, interface{})
Get(interface{}) (interface{}, error)
GetIFPresent(interface{}) (interface{}, error)
GetALL() map[interface{}]interface{}
get(interface{}) (interface{}, error)
Remove(interface{}) bool
Purge()
Keys() []interface{}
Len() int
gc()
}
type baseCache struct {
size int
loaderFunc *LoaderFunc
evictedFunc *EvictedFunc
addedFunc *AddedFunc
expiration *time.Duration
mu sync.RWMutex
loadGroup Group
}
type LoaderFunc func(interface{}) (interface{}, error)
type EvictedFunc func(interface{}, interface{})
type AddedFunc func(interface{}, interface{})
type CacheBuilder struct {
tp string
size int
loaderFunc *LoaderFunc
evictedFunc *EvictedFunc
addedFunc *AddedFunc
expiration *time.Duration
gcInterval *time.Duration
}
func New(size int) *CacheBuilder {
if size <= 0 {
panic("gcache: size <= 0")
}
return &CacheBuilder{
tp: TYPE_SIMPLE,
size: size,
}
}
// Set a loader function.
// loaderFunc: create a new value with this function if cached value is expired.
func (cb *CacheBuilder) LoaderFunc(loaderFunc LoaderFunc) *CacheBuilder {
cb.loaderFunc = &loaderFunc
return cb
}
func (cb *CacheBuilder) EnableGC(interval time.Duration) *CacheBuilder {
cb.gcInterval = &interval
return cb
}
func (cb *CacheBuilder) EvictType(tp string) *CacheBuilder {
cb.tp = tp
return cb
}
func (cb *CacheBuilder) Simple() *CacheBuilder {
return cb.EvictType(TYPE_SIMPLE)
}
func (cb *CacheBuilder) LRU() *CacheBuilder {
return cb.EvictType(TYPE_LRU)
}
func (cb *CacheBuilder) LFU() *CacheBuilder {
return cb.EvictType(TYPE_LFU)
}
func (cb *CacheBuilder) ARC() *CacheBuilder {
return cb.EvictType(TYPE_ARC)
}
func (cb *CacheBuilder) EvictedFunc(evictedFunc EvictedFunc) *CacheBuilder {
cb.evictedFunc = &evictedFunc
return cb
}
func (cb *CacheBuilder) AddedFunc(addedFunc AddedFunc) *CacheBuilder {
cb.addedFunc = &addedFunc
return cb
}
func (cb *CacheBuilder) Expiration(expiration time.Duration) *CacheBuilder {
cb.expiration = &expiration
return cb
}
func (cb *CacheBuilder) Build() Cache {
cache := cb.build()
if cb.gcInterval != nil {
go func() {
t := time.NewTicker(*cb.gcInterval)
for {
select {
case <-t.C:
go cache.gc()
}
}
t.Stop()
}()
}
return cache
}
func (cb *CacheBuilder) build() Cache {
switch cb.tp {
case TYPE_SIMPLE:
return newSimpleCache(cb)
case TYPE_LRU:
return newLRUCache(cb)
case TYPE_LFU:
return newLFUCache(cb)
case TYPE_ARC:
return newARC(cb)
default:
panic("gcache: Unknown type " + cb.tp)
}
}
func buildCache(c *baseCache, cb *CacheBuilder) {
c.size = cb.size
c.loaderFunc = cb.loaderFunc
c.expiration = cb.expiration
c.addedFunc = cb.addedFunc
c.evictedFunc = cb.evictedFunc
}
// load a new value using by specified key.
func (c *baseCache) load(key interface{}, cb func(interface{}, error) (interface{}, error), isWait bool) (interface{}, bool, error) {
v, called, err := c.loadGroup.Do(key, func() (interface{}, error) {
return cb((*c.loaderFunc)(key))
}, isWait)
if err != nil {
return nil, called, err
}
return v, called, nil
}