forked from bradberger/gocache
/
gocache.go
343 lines (292 loc) · 7.84 KB
/
gocache.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Package gocache implements a set of drivers and a common interface for working with different cache systems
package gocache
import (
"errors"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/rubanbydesign/gocache/cache"
"stathat.com/c/consistent"
)
const (
// ReplicateAsync indicates that replication will be done asyncronously.
// Set commands will return without error as soon as at least one node has
// the value
ReplicateAsync ReplicationMethod = iota
// ReplicateSync indicates that replication will be done syncronously.
// Set commands will return without error only if all nodes return without error
ReplicateSync = iota
// RetrieveSync indicates that get commands(get, exists) will be send to one
// node at a time starting with the closest node, and the first result will be used
RetrieveSync RetrievalMethod = iota
// RetrieveAsync indicates that get commands(get, exists) will be send in parallel
// to the given nodes, and the first result will be used
RetrieveAsync = iota
)
var (
// ErrGetTimeout indicates a retrieval command timed out
ErrGetTimeout = errors.New("get timeout")
// ErrSetTimeout indicates a storage command timed out
ErrSetTimeout = errors.New("set timeout")
_ cache.Cache = (*Client)(nil)
)
// ReplicationMethod determines whether replication takes place asyncronously or syncronously.
// Use ReplicateAsync for asyncronous replication, ReplicateSync for syncronous replication.
type ReplicationMethod int
// RetrievalMethod determines whether get commands (get, exists) are sent to all nodes
// simultaneously or are sent one-by-one.
type RetrievalMethod int
// Client is a cache client with built in replication to any number of different caches.
// This allows replication and syncronization across various caches using the set of drivers
// available as subpackages, including Memcached, Redis, in-memory caches, and more.
type Client struct {
nodes map[string]cache.Cache
ch *consistent.Consistent
replicateNodeCt int
replicateMethod ReplicationMethod
retrievalMethod RetrievalMethod
timeoutSet time.Duration
timeoutGet time.Duration
sync.RWMutex
}
// New returns a new initialized cache Client with no nodes.
func New() *Client {
return &Client{
nodes: make(map[string]cache.Cache, 0),
ch: consistent.New(),
replicateMethod: ReplicateAsync,
timeoutSet: time.Millisecond * 200,
timeoutGet: time.Millisecond * 200,
}
}
// AddNode adds a cache node with the given name, but only if it doesn't already exist
func (c *Client) AddNode(name string, node cache.Cache) error {
c.RLock()
_, exists := c.nodes[name]
c.RUnlock()
if exists {
return errors.New("node already exists")
}
return c.SetNode(name, node)
}
// SetNode sets the cache node with the given name, regardless of whether it already exists or not
func (c *Client) SetNode(name string, node cache.Cache) error {
if node == nil {
return errors.New("cache node is nil")
}
c.Lock()
c.nodes[name] = node
c.ch.Add(name)
c.Unlock()
return nil
}
// GetTimeout updates the get command timeout duration
func (c *Client) GetTimeout(dur time.Duration) {
c.Lock()
c.timeoutGet = dur
c.Unlock()
}
// SetTimeout updates the set command timeout duration
func (c *Client) SetTimeout(dur time.Duration) {
c.Lock()
c.timeoutSet = dur
c.Unlock()
}
// ReplaceNode adds a cache node with the given name, but only if it already exists
func (c *Client) ReplaceNode(name string, node cache.Cache) error {
c.RLock()
_, exists := c.nodes[name]
c.RUnlock()
if !exists {
return errors.New("node does not exist")
}
return c.SetNode(name, node)
}
// RemoveNode removes a node with the given name from the node list
func (c *Client) RemoveNode(name string) error {
c.Lock()
defer c.Unlock()
delete(c.nodes, name)
c.ch.Remove(name)
return nil
}
// SetReplicateMethod sets the replication method
func (c *Client) SetReplicateMethod(m ReplicationMethod) {
c.Lock()
c.replicateMethod = m
c.Unlock()
}
// SetRetrievalMethod sets the retrieval method
func (c *Client) SetRetrievalMethod(m RetrievalMethod) {
c.retrievalMethod = m
}
// ReplicateToN sets how many nodes each key should be replicated to
func (c *Client) ReplicateToN(numNodes int) error {
if numNodes > len(c.ch.Members()) {
return errors.New("invalid number of nodes")
}
c.Lock()
c.replicateNodeCt = numNodes
c.Unlock()
return nil
}
func (c *Client) node(nodeName string) cache.Cache {
c.RLock()
defer c.RUnlock()
return c.nodes[nodeName]
}
// Set implements the "cache.Cache".Set() interface
func (c *Client) Set(key string, value interface{}, exp time.Duration) (err error) {
nodes, err := c.ch.GetN(key, c.replicateNodeCt)
if err != nil {
return
}
if c.replicateMethod == ReplicateSync {
var eg errgroup.Group
for i := range nodes {
nodeName := nodes[i]
eg.Go(func() error {
return c.node(nodeName).Set(key, value, exp)
})
}
return eg.Wait()
}
err = c.node(nodes[0]).Set(key, value, exp)
if len(nodes) > 1 {
nodes = nodes[1:]
for i := range nodes {
go c.node(nodes[i]).Set(key, value, exp)
}
}
return
}
// Get implements the "cache.Cache".Get() interface. It checks nodes in order
// of priority, and returns success if the value exists on any of them.
func (c *Client) Get(key string, dstVal interface{}) (err error) {
nodes, err := c.ch.GetN(key, c.replicateNodeCt)
if err != nil {
return err
}
// asyncronously get the key from the first node which has it
if c.retrievalMethod == RetrieveAsync {
done := make(chan error, 1)
timeout := make(chan struct{}, 1)
c.RLock()
to := c.timeoutGet
c.RUnlock()
if to > 0 {
go func() {
time.Sleep(to)
timeout <- struct{}{}
}()
}
go func() {
var wg sync.WaitGroup
wg.Add(len(c.nodes))
for i := range c.nodes {
go func(nm string) {
// This is for unit testing, nobody would actually set a 1ns timeout, right?
if to == time.Nanosecond {
time.Sleep(time.Millisecond)
}
if e := c.node(nm).Get(key, dstVal); e == nil {
done <- nil
}
wg.Done()
}(i)
}
wg.Wait()
done <- cache.ErrCacheMiss
}()
select {
case <-timeout:
return ErrGetTimeout
case err = <-done:
return err
}
}
// syncronous mode
for i := range nodes {
if err = c.node(nodes[i]).Get(key, dstVal); err == nil {
return
}
}
return cache.ErrCacheMiss
}
// Exists implements the "cache.Cache".Exists() interface
func (c *Client) Exists(key string) (exists bool) {
timeout := make(chan struct{}, 1)
found := make(chan bool, 1)
go func() {
if c.timeoutGet > 0 {
time.Sleep(c.timeoutGet)
timeout <- struct{}{}
}
}()
go func() {
nodes, err := c.ch.GetN(key, c.replicateNodeCt)
if err != nil {
found <- false
return
}
if c.retrievalMethod == RetrieveAsync {
var wg sync.WaitGroup
wg.Add(len(c.nodes))
for i := range nodes {
go func(nm string) {
if c.node(nm).Exists(key) {
found <- true
}
wg.Done()
}(nodes[i])
}
wg.Wait()
found <- false
return
}
for i := range nodes {
if c.node(nodes[i]).Exists(key) {
found <- true
}
}
}()
select {
case <-timeout:
return false
case exists = <-found:
return
}
}
// Del implements the "cache.Cache".Del() interface. It deletes the given key across
// all replicated nodes and returns error if any of those delete operations fail.
func (c *Client) Del(key string) (err error) {
var nodes []string
c.RLock()
to := c.timeoutSet
rn := c.replicateNodeCt
nodes, err = c.ch.GetN(key, rn)
if err != nil {
return err
}
c.RUnlock()
ec := make(chan error, 1)
go func() {
var eg errgroup.Group
for i := range nodes {
nm := nodes[i]
eg.Go(func() error {
if to == time.Nanosecond {
time.Sleep(time.Millisecond)
}
return c.node(nm).Del(key)
})
}
ec <- eg.Wait()
}()
select {
case err = <-ec:
return
case <-time.After(to):
return ErrSetTimeout
}
}