-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathcache.go
137 lines (108 loc) · 2.21 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
package agollo
import (
"encoding/gob"
"os"
"sync"
)
type namespaceCache struct {
lock sync.Mutex
caches map[string]*cache
}
func newNamespaceCache() *namespaceCache {
return &namespaceCache{
caches: map[string]*cache{},
}
}
func (n *namespaceCache) mustGetCache(namespace string) *cache {
n.lock.Lock()
defer n.lock.Unlock()
if ret, ok := n.caches[namespace]; ok {
return ret
}
cache := newCache()
n.caches[namespace] = cache
return cache
}
func (n *namespaceCache) drain() {
n.lock.Lock()
defer n.lock.Unlock()
for namespace := range n.caches {
delete(n.caches, namespace)
}
}
func (n *namespaceCache) cacheDump() map[string]map[string]string {
n.lock.Lock()
defer n.lock.Unlock()
var dumps = map[string]map[string]string{}
for namespace, cache := range n.caches {
dumps[namespace] = cache.dump()
}
return dumps
}
func (n *namespaceCache) dump(name string) error {
dumps := n.cacheDump()
tmp := name + "tmp"
f, err := os.OpenFile(tmp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
if err := gob.NewEncoder(f).Encode(&dumps); err != nil {
_ = f.Close()
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(tmp, name)
}
func (n *namespaceCache) load(name string) error {
n.drain()
f, err := os.OpenFile(name, os.O_RDONLY, 0755)
if err != nil {
return err
}
defer f.Close()
var dumps = map[string]map[string]string{}
if err := gob.NewDecoder(f).Decode(&dumps); err != nil {
return err
}
for namespace, kv := range dumps {
cache := n.mustGetCache(namespace)
for k, v := range kv {
cache.set(k, v)
}
}
return nil
}
type cache struct {
kv sync.Map
}
func newCache() *cache {
return &cache{
kv: sync.Map{},
}
}
func (c *cache) set(key, val string) {
c.kv.Store(key, val)
}
func (c *cache) get(key string) (string, bool) {
if val, ok := c.kv.Load(key); ok {
if ret, ok := val.(string); ok {
return ret, true
}
}
return "", false
}
func (c *cache) delete(key string) {
c.kv.Delete(key)
}
func (c *cache) dump() map[string]string {
var ret = map[string]string{}
c.kv.Range(func(key, val interface{}) bool {
k, _ := key.(string)
v, _ := val.(string)
ret[k] = v
return true
})
return ret
}