forked from philchia/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
121 lines (96 loc) · 1.95 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
package agollo
import (
"encoding/gob"
"os"
"sync"
)
type namespaceCache struct {
lock sync.RWMutex
caches map[string]*cache
}
func newNamespaceCahce() *namespaceCache {
return &namespaceCache{
caches: map[string]*cache{},
}
}
func (n *namespaceCache) mustGetCache(namespace string) *cache {
n.lock.RLock()
if ret, ok := n.caches[namespace]; ok {
n.lock.RUnlock()
return ret
}
n.lock.RUnlock()
n.lock.Lock()
defer n.lock.Unlock()
cache := newCache()
n.caches[namespace] = cache
return cache
}
func (n *namespaceCache) drain() {
for namespace := range n.caches {
delete(n.caches, namespace)
}
}
func (n *namespaceCache) dump(name string) error {
var dumps = map[string]map[string]string{}
for namespace, cache := range n.caches {
dumps[namespace] = cache.dump()
}
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer f.Close()
return gob.NewEncoder(f).Encode(&dumps)
}
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
}