forked from mercari/datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsmemcache.go
executable file
·144 lines (119 loc) · 3.49 KB
/
dsmemcache.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
package dsmemcache
import (
"bytes"
"context"
"encoding/gob"
"time"
"github.com/bradfitz/gomemcache/memcache"
"go.mercari.io/datastore"
"go.mercari.io/datastore/dsmiddleware/storagecache"
)
var _ storagecache.Storage = &cacheHandler{}
var _ datastore.Middleware = &cacheHandler{}
// New dsmemcache middleware creates & returns.
func New(client *memcache.Client, opts ...CacheOption) interface {
datastore.Middleware
storagecache.Storage
} {
ch := &cacheHandler{
client: client,
stOpts: &storagecache.Options{},
}
for _, opt := range opts {
opt.Apply(ch)
}
s := storagecache.New(ch, ch.stOpts)
ch.Middleware = s
if ch.logf == nil {
ch.logf = func(ctx context.Context, format string, args ...interface{}) {}
}
if ch.cacheKey == nil {
ch.cacheKey = func(key datastore.Key) string {
return "mercari:dsmemcache:" + key.Encode()
}
}
return ch
}
type cacheHandler struct {
datastore.Middleware
stOpts *storagecache.Options
client *memcache.Client
expireDuration time.Duration
logf func(ctx context.Context, format string, args ...interface{})
cacheKey func(key datastore.Key) string
}
// A CacheOption is an cache option for a dsmemcache middleware.
type CacheOption interface {
Apply(*cacheHandler)
}
func (ch *cacheHandler) SetMulti(ctx context.Context, cis []*storagecache.CacheItem) error {
ch.logf(ctx, "dsmiddleware/dsmemcache.SetMulti: incoming len=%d", len(cis))
for _, ci := range cis {
if ci.Key.Incomplete() {
panic("incomplete key incoming")
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(ci.PropertyList); err != nil {
ch.logf(ctx, "dsmiddleware/dsmemcache.SetMulti: gob.Encode error key=%s err=%s", ci.Key.String(), err.Error())
continue
}
item := &memcache.Item{
Key: ch.cacheKey(ci.Key),
Value: buf.Bytes(),
Expiration: int32(ch.expireDuration.Seconds()),
}
if err := ch.client.Set(item); err != nil {
return err
}
}
return nil
}
func (ch *cacheHandler) GetMulti(ctx context.Context, keys []datastore.Key) ([]*storagecache.CacheItem, error) {
ch.logf(ctx, "dsmiddleware/dsmemcache.GetMulti: incoming len=%d", len(keys))
resultList := make([]*storagecache.CacheItem, len(keys))
cacheKeys := make([]string, 0, len(keys))
for _, key := range keys {
cacheKeys = append(cacheKeys, ch.cacheKey(key))
}
itemMap, err := ch.client.GetMulti(cacheKeys)
if err != nil {
ch.logf(ctx, "dsmiddleware/dsmemcache: error on dsmemcache.GetMulti %s", err.Error())
}
hit, miss := 0, 0
for idx, key := range keys {
item, ok := itemMap[ch.cacheKey(key)]
if !ok {
resultList[idx] = nil
miss++
continue
}
buf := bytes.NewBuffer(item.Value)
dec := gob.NewDecoder(buf)
var ps datastore.PropertyList
err = dec.Decode(&ps)
if err != nil {
resultList[idx] = nil
ch.logf(ctx, "dsmiddleware/dsmemcache.GetMulti: gob.Decode error key=%s err=%s", key.String(), err.Error())
miss++
continue
}
resultList[idx] = &storagecache.CacheItem{
Key: key,
PropertyList: ps,
}
hit++
}
ch.logf(ctx, "dsmiddleware/dsmemcache.GetMulti: hit=%d miss=%d", hit, miss)
return resultList, nil
}
func (ch *cacheHandler) DeleteMulti(ctx context.Context, keys []datastore.Key) error {
ch.logf(ctx, "dsmiddleware/dsmemcache.DeleteMulti: incoming len=%d", len(keys))
for _, key := range keys {
err := ch.client.Delete(ch.cacheKey(key))
if err != nil {
ch.logf(ctx, "dsmiddleware/dsmemcache: error on dsmemcache.DeleteMulti %s", err.Error())
}
}
return nil
}