-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticcache.go
160 lines (130 loc) · 3.59 KB
/
staticcache.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
package staticcache
import (
"github.com/YuHover/staticcache/lru"
"github.com/YuHover/staticcache/singleflight"
"sync"
)
// StaticCache provides static resource cache service.
type StaticCache struct {
name string
limit int64
mainCache *syncLRU
getter LocalGetter
picker *ConsistentPicker
throttle *singleflight.SingleFlight
}
var caches = make(map[string]*StaticCache)
func NewStaticCache(name string, limit int64, getter LocalGetter) *StaticCache {
if getter == nil {
panic("must provide local getter")
}
if _, dup := caches[name]; dup {
panic("duplicate registration of cache: " + name)
}
sc := &StaticCache{
name: name,
getter: getter,
mainCache: &syncLRU{},
limit: limit,
throttle: &singleflight.SingleFlight{},
}
caches[name] = sc
return sc
}
func GetCache(name string) *StaticCache {
return caches[name]
}
func (sc *StaticCache) SetConsistentPicker(picker *ConsistentPicker) {
sc.picker = picker
}
func (sc *StaticCache) Get(key string) (byteView, error) {
if bv, hit := sc.mainCache.get(key); hit {
return bv, nil
}
// single flight
bv, err := sc.throttle.Throttle(key, func() (any, error) {
if sc.picker != nil {
if remote := sc.picker.PickServer(key); remote != nil {
if bv, err := sc.getFromRemote(remote, key); err == nil {
return bv, nil
}
}
}
return sc.getLocally(key)
})
if err == nil {
return bv.(byteView), nil
}
return byteView{}, err
}
func (sc *StaticCache) getFromRemote(remote CacheServer, key string) (byteView, error) {
bs, err := remote.Get(sc.name, key)
if err != nil {
return byteView{}, err
}
return byteView{bs: bs}, nil
}
func (sc *StaticCache) getLocally(key string) (byteView, error) {
bs, err := sc.getter.Get(key)
if err != nil {
return byteView{}, err
}
bv := byteView{bs: cloneBytes(bs)}
sc.populateCache(key, bv)
return bv, nil
}
func (sc *StaticCache) populateCache(key string, value byteView) {
sc.mainCache.add(key, value)
for sc.mainCache.bytes() > sc.limit {
sc.mainCache.removeOldest()
}
}
// syncLRU is a concurrent secure LRUCache
type syncLRU struct {
mu sync.RWMutex
lru *lru.LRUCache[string]
nbytes int64
}
func (sl *syncLRU) add(key string, value byteView) {
sl.mu.Lock()
defer sl.mu.Unlock()
if sl.lru == nil {
sl.lru = lru.New[string](0, func(key string, value any) {
sl.nbytes -= int64(len(key)) + int64(value.(byteView).Len())
})
}
sl.lru.Add(key, value)
sl.nbytes += int64(len(key)) + int64(value.Len())
}
func (sl *syncLRU) get(key string) (byteView, bool) {
sl.mu.RLock()
defer sl.mu.RUnlock()
if sl.lru == nil {
return byteView{}, false
}
if val, ok := sl.lru.Get(key); ok {
return val.(byteView), ok
}
return byteView{}, false
}
func (sl *syncLRU) removeOldest() {
sl.mu.Lock()
defer sl.mu.Unlock()
if sl.lru != nil {
sl.lru.RemoveOldest()
}
}
func (sl *syncLRU) bytes() int64 {
sl.mu.RLock()
defer sl.mu.RUnlock()
return sl.nbytes
}
// LocalGetter is the final data source.
// When both the local cache and remote cache miss, trying to get data from LocalGetter.
type LocalGetter interface {
Get(key string) ([]byte, error)
}
type GetterFunc func(key string) ([]byte, error)
func (f GetterFunc) Get(key string) ([]byte, error) {
return f(key)
}