1
|
|
|
// Package weakcache implements Weak Cache, eviction of entries are controlled by GC |
2
|
|
|
package weakcache |
3
|
|
|
|
4
|
|
|
import ( |
5
|
|
|
"encoding/json" |
6
|
|
|
"fmt" |
7
|
|
|
"github.com/arazmj/gerdu/cache" |
8
|
|
|
"github.com/arazmj/gerdu/metrics" |
9
|
|
|
"github.com/hashicorp/raft" |
10
|
|
|
"github.com/ivanrad/go-weakref/weakref" |
11
|
|
|
"io" |
12
|
|
|
"sync" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
// WeakCache data structure |
16
|
|
|
type WeakCache struct { |
17
|
|
|
sync.Map |
18
|
|
|
cache.UnImplementedCache |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
// NewWeakCache constructor |
22
|
|
|
func NewWeakCache() *WeakCache { |
23
|
|
|
return &WeakCache{} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Put a new key value pair |
27
|
|
|
func (c *WeakCache) Put(key string, value string) (created bool) { |
28
|
|
|
metrics.Adds.Inc() |
29
|
|
|
ref := weakref.NewWeakRef(value) |
30
|
|
|
c.Store(key, ref) |
31
|
|
|
return true |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Get value by key |
35
|
|
|
func (c *WeakCache) Get(key string) (value string, ok bool) { |
36
|
|
|
v, ok := c.Load(key) |
37
|
|
|
if ok { |
38
|
|
|
ref := v.(*weakref.WeakRef) |
39
|
|
|
if ref.IsAlive() { |
40
|
|
|
metrics.Hits.Inc() |
41
|
|
|
return ref.GetTarget().(string), true |
42
|
|
|
} |
43
|
|
|
metrics.Deletes.Inc() |
44
|
|
|
c.Delete(key) |
45
|
|
|
} |
46
|
|
|
metrics.Miss.Inc() |
47
|
|
|
return "", false |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
//Delete deletes the key |
51
|
|
|
func (c *WeakCache) Delete(key string) bool { |
52
|
|
|
metrics.Deletes.Inc() |
53
|
|
|
c.Map.Delete(key) |
54
|
|
|
return true |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
func (c *WeakCache) Snapshot() (raft.FSMSnapshot, error) { |
58
|
|
|
o := make(map[string]string) |
59
|
|
|
|
60
|
|
|
c.Map.Range(func(key, value interface{}) bool { |
61
|
|
|
ref := value.(*weakref.WeakRef) |
62
|
|
|
if ref.IsAlive() { |
63
|
|
|
metrics.Hits.Inc() |
64
|
|
|
o[fmt.Sprint(key)] = ref.GetTarget().(string) |
65
|
|
|
} |
66
|
|
|
return true |
67
|
|
|
}) |
68
|
|
|
|
69
|
|
|
return &fsmSnapshot{store: o}, nil |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
func (c *WeakCache) Restore(closer io.ReadCloser) error { |
74
|
|
|
o := make(map[string]string) |
75
|
|
|
if err := json.NewDecoder(closer).Decode(&o); err != nil { |
76
|
|
|
return err |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Set the state from the snapshot, no lock required according to |
80
|
|
|
// Hashicorp docs. |
81
|
|
|
for k, v := range o { |
82
|
|
|
c.Put(k, v) |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return nil |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
type fsmSnapshot struct { |
89
|
|
|
store map[string]string |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error { |
93
|
|
|
err := func() error { |
94
|
|
|
// Encode data. |
95
|
|
|
b, err := json.Marshal(f.store) |
96
|
|
|
if err != nil { |
97
|
|
|
return err |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Write data to sink. |
101
|
|
|
if _, err := sink.Write(b); err != nil { |
102
|
|
|
return err |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Close the sink. |
106
|
|
|
return sink.Close() |
107
|
|
|
}() |
108
|
|
|
|
109
|
|
|
if err != nil { |
110
|
|
|
sink.Cancel() |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return err |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
func (f *fsmSnapshot) Release() {} |
117
|
|
|
|