-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcached.go
88 lines (68 loc) · 1.7 KB
/
memcached.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
package memcached
import (
"strings"
"github.com/golang-plus/caching"
"github.com/golang-plus/errors"
"github.com/rainycape/memcache"
)
type Container struct {
client *memcache.Client
}
func (c *Container) Clear() error {
err := c.client.Flush(0)
if err != nil {
return errors.Wrap(err, "could not clear container")
}
return nil
}
func (c *Container) Remove(key string) error {
err := c.client.Delete(key)
if err != nil && err != memcache.ErrCacheMiss {
return errors.Wrapf(err, "could not remove item with key %q from container", key)
}
return nil
}
func (c *Container) Put(item *caching.Item) error {
data, err := item.Marshal()
if err != nil {
return err
}
mci := &memcache.Item{
Key: item.Key,
Value: data,
}
err = c.client.Set(mci)
if err != nil {
return errors.Wrapf(err, "could not put (insert/update) item with key %q to container", item.Key)
}
return nil
}
func (c *Container) Get(key string) (*caching.Item, error) {
mci, err := c.client.Get(key)
if err != nil {
if err == memcache.ErrCacheMiss {
return nil, nil
}
return nil, errors.Wrapf(err, "could not get item with key %s from container", key)
}
var item *caching.Item
err = item.Unmarshal(mci.Value)
if err != nil {
return nil, err
}
return item, nil
}
// NewContainer returns a new memcached cache container.
func NewContainer(servers ...string) (caching.Container, error) {
client, err := memcache.New(servers...)
if err != nil {
return nil, errors.Wrapf(err, "could not connect to servers %q", strings.Join(servers, ","))
}
return &Container{
client: client,
}, nil
}
// Short to NewContainer func.
func New(servers ...string) (caching.Container, error) {
return NewContainer(servers...)
}