forked from gin-contrib/cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcached.go
99 lines (82 loc) · 2.59 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
89
90
91
92
93
94
95
96
97
98
99
package persistence
import (
"time"
"github.com/bradfitz/gomemcache/memcache"
"github.com/schollz/gincache/utils"
)
// MemcachedStore represents the cache with memcached persistence
type MemcachedStore struct {
*memcache.Client
defaultExpiration time.Duration
}
// NewMemcachedStore returns a MemcachedStore
func NewMemcachedStore(hostList []string, defaultExpiration time.Duration) *MemcachedStore {
return &MemcachedStore{memcache.New(hostList...), defaultExpiration}
}
// Set (see CacheStore interface)
func (c *MemcachedStore) Set(key string, value interface{}, expires time.Duration) error {
return c.invoke((*memcache.Client).Set, key, value, expires)
}
// Add (see CacheStore interface)
func (c *MemcachedStore) Add(key string, value interface{}, expires time.Duration) error {
return c.invoke((*memcache.Client).Add, key, value, expires)
}
// Replace (see CacheStore interface)
func (c *MemcachedStore) Replace(key string, value interface{}, expires time.Duration) error {
return c.invoke((*memcache.Client).Replace, key, value, expires)
}
// Get (see CacheStore interface)
func (c *MemcachedStore) Get(key string, value interface{}) error {
item, err := c.Client.Get(key)
if err != nil {
return convertMemcacheError(err)
}
return utils.Deserialize(item.Value, value)
}
// Delete (see CacheStore interface)
func (c *MemcachedStore) Delete(key string) error {
return convertMemcacheError(c.Client.Delete(key))
}
// Increment (see CacheStore interface)
func (c *MemcachedStore) Increment(key string, delta uint64) (uint64, error) {
newValue, err := c.Client.Increment(key, delta)
return newValue, convertMemcacheError(err)
}
// Decrement (see CacheStore interface)
func (c *MemcachedStore) Decrement(key string, delta uint64) (uint64, error) {
newValue, err := c.Client.Decrement(key, delta)
return newValue, convertMemcacheError(err)
}
// Flush (see CacheStore interface)
func (c *MemcachedStore) Flush() error {
return ErrNotSupport
}
func (c *MemcachedStore) invoke(storeFn func(*memcache.Client, *memcache.Item) error,
key string, value interface{}, expire time.Duration) error {
switch expire {
case DEFAULT:
expire = c.defaultExpiration
case FOREVER:
expire = time.Duration(0)
}
b, err := utils.Serialize(value)
if err != nil {
return err
}
return convertMemcacheError(storeFn(c.Client, &memcache.Item{
Key: key,
Value: b,
Expiration: int32(expire / time.Second),
}))
}
func convertMemcacheError(err error) error {
switch err {
case nil:
return nil
case memcache.ErrCacheMiss:
return ErrCacheMiss
case memcache.ErrNotStored:
return ErrNotStored
}
return err
}