-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcache.go
218 lines (182 loc) · 6.35 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
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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package memcache // import "aahframe.work/cache/provider/memcache"
import (
"bytes"
"encoding/gob"
"fmt"
"strings"
"sync"
"time"
"aahframe.work/cache"
"aahframe.work/config"
"aahframe.work/log"
"github.com/bradfitz/gomemcache/memcache"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Provider and its exported methods
//______________________________________________________________________________
// Provider struct represents the Redis cache provider.
type Provider struct {
name string
logger log.Loggerer
cfg *cache.Config
appCfg *config.Config
client *memcache.Client
}
var _ cache.Provider = (*Provider)(nil)
// Init method initializes the Redis cache provider.
func (p *Provider) Init(providerName string, appCfg *config.Config, logger log.Loggerer) error {
p.name = providerName
p.appCfg = appCfg
p.logger = logger.WithField("cache_provider", providerName)
cfgPrefix := "cache." + p.name + "."
if strings.ToLower(p.appCfg.StringDefault(cfgPrefix+"provider", "")) != "memcache" {
return fmt.Errorf("aah/cache: not a vaild provider name, expected 'memcache'")
}
addresses, found := p.appCfg.StringList(cfgPrefix + "addresses")
if !found {
addresses = []string{"0.0.0.0:11211"}
}
p.client = memcache.New(addresses...)
p.client.MaxIdleConns = p.appCfg.IntDefault(cfgPrefix+"max_idle_conns", memcache.DefaultMaxIdleConns)
p.client.Timeout = parseDuration(p.appCfg.StringDefault(cfgPrefix+"timeout", "5s"), "5s")
gob.Register(entry{})
// Check server connection
if _, err := p.client.Get(p.name + "-testkey"); err != nil && err != memcache.ErrCacheMiss {
return fmt.Errorf("aah/cache/%s: %s", p.name, err)
}
p.logger.Infof("aah/cache/provider: %s connected successfully with %s", p.name, strings.Join(addresses, ", "))
return nil
}
// Create method creates new Redis cache with given options.
func (p *Provider) Create(cfg *cache.Config) (cache.Cache, error) {
p.cfg = cfg
m := &memcacheCache{
keyPrefix: p.cfg.Name + "-",
p: p,
}
return m, nil
}
// Client method returns underlying memcache client. So that aah user could perform
// cache provider specific features.
func (p *Provider) Client() *memcache.Client {
return p.client
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// memcacheCache struct implements `cache.Cache` interface.
//______________________________________________________________________________
type memcacheCache struct {
keyPrefix string
p *Provider
}
var _ cache.Cache = (*memcacheCache)(nil)
// Name method returns the cache store name.
func (m *memcacheCache) Name() string {
return m.p.cfg.Name
}
// Get method returns the cached entry for given key if it exists otherwise nil.
// Method uses `gob.Decoder` to unmarshal cache value from bytes.
func (m *memcacheCache) Get(k string) interface{} {
k = m.keyPrefix + k
v, err := m.p.client.Get(k)
if err != nil {
// if notacacheMiss(err) != nil {
m.p.logger.Errorf("aah/cache/%s: key(%s) %v", m.Name(), k[len(m.keyPrefix):], err)
// }
return nil
}
var e entry
err = gob.NewDecoder(bytes.NewBuffer(v.Value)).Decode(&e)
if err != nil {
m.p.logger.Errorf("aah/cache/%s: %v", m.Name(), err)
return nil
}
if m.p.cfg.EvictionMode == cache.EvictionModeSlide {
if err = m.p.client.Touch(k, e.D); err != nil {
m.p.logger.Errorf("aah/cache/%s: key(%s) %v", m.Name(), k[len(m.keyPrefix):], err)
}
}
return e.V
}
// GetOrPut method returns the cached entry for the given key if it exists otherwise
// it puts the new entry into cache store and returns the value.
func (m *memcacheCache) GetOrPut(k string, v interface{}, d time.Duration) (interface{}, error) {
ev := m.Get(k)
if ev == nil {
if err := m.Put(k, v, d); err != nil {
return nil, err
}
return v, nil
}
return ev, nil
}
// Put method adds the cache entry with specified expiration. Returns error
// if cache entry exists. Method uses `gob.Encoder` to marshal cache value into bytes.
func (m *memcacheCache) Put(k string, v interface{}, d time.Duration) error {
e := entry{D: int32(d.Seconds()), V: v}
buf := acquireBuffer()
enc := gob.NewEncoder(buf)
if err := enc.Encode(e); err != nil {
return fmt.Errorf("aah/cache/%s: %v", m.Name(), err)
}
err := m.p.client.Set(&memcache.Item{
Key: m.keyPrefix + k,
Value: buf.Bytes(),
Expiration: e.D,
})
releaseBuffer(buf)
return err
}
// Delete method deletes the cache entry from cache store.
func (m *memcacheCache) Delete(k string) error {
if err := m.p.client.Delete(m.keyPrefix + k); notacacheMiss(err) != nil {
return fmt.Errorf("aah/cache/%s: key(%s) %v", m.Name(), k, err)
}
return nil
}
// Exists method checks given key exists in cache store and its not expried.
func (m *memcacheCache) Exists(k string) bool {
if v := m.Get(k); v != nil {
return true
}
return false
}
// Flush methods flushes(deletes) all the cache entries from cache.
func (m *memcacheCache) Flush() error {
if err := m.p.client.FlushAll(); err != nil {
return fmt.Errorf("aah/cache/%s: %v", m.Name(), err)
}
return nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Helper methods
//______________________________________________________________________________
type entry struct {
D int32
V interface{}
}
func parseDuration(v, f string) time.Duration {
if d, err := time.ParseDuration(v); err == nil {
return d
}
d, _ := time.ParseDuration(f)
return d
}
var bufPool = sync.Pool{New: func() interface{} { return new(bytes.Buffer) }}
func acquireBuffer() *bytes.Buffer {
return bufPool.Get().(*bytes.Buffer)
}
func releaseBuffer(b *bytes.Buffer) {
if b != nil {
b.Reset()
bufPool.Put(b)
}
}
func notacacheMiss(err error) error {
if err == memcache.ErrCacheMiss {
return nil
}
return nil
}