forked from yoavfeld/nds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemcache.go
87 lines (79 loc) · 1.98 KB
/
memcache.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
package nds
import (
"golang.org/x/net/context"
"github.com/bradfitz/gomemcache/memcache"
"cloud.google.com/go/datastore"
)
type memcacheClient struct {
*memcache.Client
}
func NewMemcache(addr string) *memcacheClient{
return &memcacheClient{memcache.New(addr)}
}
func (mc *memcacheClient) AddMulti(c context.Context, items []*memcache.Item) error {
if McClient == nil {
panic("memcache was not initialized. did you call nds.Init ?")
}
multiErr, any := make(datastore.MultiError, len(items)), false
for i, item := range items {
if err := mc.Add(item); err != nil {
multiErr[i] = err
any = true
}
}
if any {
return multiErr
}
return nil
}
func (mc *memcacheClient) SetMulti(c context.Context, items []*memcache.Item) error {
if McClient == nil {
panic("memcache was not initialized. did you call nds.Init ?")
}
multiErr, any := make(datastore.MultiError, len(items)), false
for i, item := range items {
if err := mc.Set(item); err != nil {
multiErr[i] = err
any = true
}
}
if any {
return multiErr
}
return nil
}
func (mc *memcacheClient) GetMulti(c context.Context, keys []string) (map[string]*memcache.Item, error) {
return McClient.Client.GetMulti(keys)
}
func (mc *memcacheClient) DeleteMulti(c context.Context, keys []string) error {
if McClient == nil {
panic("memcache was not initialized. did you call nds.Init ?")
}
multiErr, any := make(datastore.MultiError, len(keys)), false
for i, key := range keys {
if err := mc.Delete(key); err != nil {
multiErr[i] = err
any = true
}
}
if any {
return multiErr
}
return nil
}
func (mc *memcacheClient) CompareAndSwapMulti(c context.Context, items []*memcache.Item) error {
if McClient == nil {
panic("memcache was not initialized. did you call nds.Init ?")
}
multiErr, any := make(datastore.MultiError, len(items)), false
for i, item := range items {
if err := mc.CompareAndSwap(item); err != nil {
multiErr[i] = err
any = true
}
}
if any {
return multiErr
}
return nil
}