/
memcache.go
142 lines (117 loc) · 3.14 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
package store
import (
"errors"
"fmt"
"strings"
time "time"
"github.com/bradfitz/gomemcache/memcache"
)
// MemcacheClientInterface represents a bradfitz/gomemcache client
type MemcacheClientInterface interface {
Get(key string) (item *memcache.Item, err error)
Set(item *memcache.Item) error
Delete(item string) error
}
const (
// MemcacheType represents the storage type as a string value
MemcacheType = "memcache"
// MemcacheTagPattern represents the tag pattern to be used as a key in specified storage
MemcacheTagPattern = "gocache_tag_%s"
)
// MemcacheStore is a store for Memcache
type MemcacheStore struct {
client MemcacheClientInterface
options *Options
}
// NewMemcache creates a new store to Memcache instance(s)
func NewMemcache(client MemcacheClientInterface, options *Options) *MemcacheStore {
if options == nil {
options = &Options{}
}
return &MemcacheStore{
client: client,
options: options,
}
}
// Get returns data stored from a given key
func (s *MemcacheStore) Get(key interface{}) (interface{}, error) {
item, err := s.client.Get(key.(string))
if err != nil {
return nil, err
}
if item == nil {
return nil, errors.New("Unable to retrieve data from memcache")
}
return item.Value, err
}
// Set defines data in Memcache for given key identifier
func (s *MemcacheStore) Set(key interface{}, value interface{}, options *Options) error {
if options == nil {
options = s.options
}
item := &memcache.Item{
Key: key.(string),
Value: value.([]byte),
Expiration: int32(options.ExpirationValue().Seconds()),
}
err := s.client.Set(item)
if err != nil {
return err
}
if tags := options.TagsValue(); len(tags) > 0 {
s.setTags(key, tags)
}
return nil
}
func (s *MemcacheStore) setTags(key interface{}, tags []string) {
for _, tag := range tags {
var tagKey = fmt.Sprintf(MemcacheTagPattern, tag)
var cacheKeys = []string{}
if result, err := s.Get(tagKey); err == nil {
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
}
var alreadyInserted = false
for _, cacheKey := range cacheKeys {
if cacheKey == key.(string) {
alreadyInserted = true
break
}
}
if !alreadyInserted {
cacheKeys = append(cacheKeys, key.(string))
}
s.Set(tagKey, []byte(strings.Join(cacheKeys, ",")), &Options{
Expiration: 720 * time.Hour,
})
}
}
// Delete removes data from Memcache for given key identifier
func (s *MemcacheStore) Delete(key interface{}) error {
return s.client.Delete(key.(string))
}
// Invalidate invalidates some cache data in Redis for given options
func (s *MemcacheStore) Invalidate(options InvalidateOptions) error {
if tags := options.TagsValue(); len(tags) > 0 {
for _, tag := range tags {
var tagKey = fmt.Sprintf(MemcacheTagPattern, tag)
result, err := s.Get(tagKey)
if err != nil {
return nil
}
var cacheKeys = []string{}
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
for _, cacheKey := range cacheKeys {
s.Delete(cacheKey)
}
}
}
return nil
}
// GetType returns the store type
func (s *MemcacheStore) GetType() string {
return MemcacheType
}