-
Notifications
You must be signed in to change notification settings - Fork 20
/
cache.go
161 lines (135 loc) · 3.41 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
package bcache
import (
"sync"
"time"
"github.com/hashicorp/golang-lru"
"github.com/weaveworks/mesh"
)
type cache struct {
peerID mesh.PeerName
mux sync.RWMutex
cc *lru.Cache
}
func newCache(maxKeys int) (*cache, error) {
cc, err := lru.New(maxKeys)
if err != nil {
return nil, err
}
return &cache{
cc: cc,
}, nil
}
// value represent cache value
type value struct {
value string
expired int64 // expiration timestamp of the value
deleted int64 // deletion timestamp of the value
}
// Set sets the value of a cache
func (c *cache) Set(key, val string, expiredTimestamp, deleted int64) {
c.cc.Add(key, value{
value: val,
expired: expiredTimestamp,
deleted: deleted,
})
}
// Delete del the value of a cache.
// returns true if the key exists in cache, false otherwise
func (c *cache) Delete(key string, deleteTimestamp int64) (string, int64, bool) {
val, ok := c.get(key)
if !ok {
return "", 0, false
}
c.Set(key, val.value, val.expired, deleteTimestamp)
return val.value, val.expired, true
}
// Get gets cache value of the given key
func (c *cache) get(key string) (*value, bool) {
cacheVal, ok := c.cc.Get(key)
if !ok {
return nil, false
}
val := cacheVal.(value)
return &val, true
}
// Get gets cache value of the given key
func (c *cache) Get(key string) (string, bool) {
val, ok := c.get(key)
if !ok {
return "", false
}
now := time.Now().UnixNano()
if now >= val.expired || (now >= val.deleted && val.deleted > 0) {
// delete the key if:
// - expired
// - deleted
c.cc.Remove(key)
return "", false
}
return val.value, val.deleted <= 0
}
func (c *cache) Messages() *message {
m := newMessage(c.peerID, c.cc.Len())
for _, k := range c.cc.Keys() {
key := k.(string)
cacheVal, ok := c.get(key)
if !ok {
continue
}
m.add(key, cacheVal.value, cacheVal.expired, cacheVal.deleted)
}
return m
}
// merges received data into state and returns a
// representation of the received data (typically a delta) for further
// propagation.
func (c *cache) mergeDelta(msg *message) (delta mesh.GossipData) {
delta, _ = c.mergeChange(msg)
return delta
}
// merges received data into state and returns "everything new
// I've just learnt", or nil if nothing in the received data was new.
func (c *cache) mergeNew(msg *message) (delta mesh.GossipData) {
var changedKey int
delta, changedKey = c.mergeChange(msg)
if changedKey == 0 {
return nil
}
return delta
}
func (c *cache) mergeChange(msg *message) (delta mesh.GossipData, changedKey int) {
c.mux.Lock()
defer c.mux.Unlock()
if len(msg.Entries) == 0 {
return
}
var existingKeys []string
for key, e := range msg.Entries {
cacheVal, ok := c.get(key)
if ok && cacheVal.expired >= e.Expired && cacheVal.deleted == e.Deleted {
// no changes:
// - key already exists
// - has bigger expiration value
// - has same deleted val
existingKeys = append(existingKeys, key)
continue
}
c.Set(key, e.Val, e.Expired, e.Deleted)
changedKey++
}
// delete key that already existed in this cache
for _, key := range existingKeys {
delete(msg.Entries, key)
}
return newMessageFromEntries(c.peerID, msg.Entries), changedKey
}
func (c *cache) mergeComplete(msg *message) {
for key, ent := range msg.Entries {
cacheVal, ok := c.get(key)
if !ok || cacheVal.expired < ent.Expired {
// if !exist in cache, set it
// if val in cache is older, set it
c.Set(key, ent.Val, ent.Expired, ent.Deleted)
}
}
}