-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcached.go
64 lines (54 loc) · 1.45 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
package cache
import (
"encoding/json"
"time"
"github.com/bradfitz/gomemcache/memcache"
)
// Memcache is a structure implemening Storage interface using
// memcached as a storage provider
type Memcache struct {
client *memcache.Client
}
// Memcached creates a new memcached storage which can be passed to
// cache.New(WithStorage(...))
func Memcached(servers ...string) Memcache {
// if servers list is empty use localhost with the default port
if len(servers) == 0 {
servers = append(servers, "localhost:11211")
}
return Memcache{
client: memcache.New(servers...),
}
}
// Write writes the given content for the given key in
// memcached storage
func (m Memcache) Write(key string, v interface{}, ttl time.Duration) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
item := &memcache.Item{Key: key, Value: b}
if ttl > 0 {
item.Expiration = int32(time.Now().Add(ttl).Unix())
}
return m.client.Set(item)
}
// Read reads coontent for the given key from in memcached storage
func (m Memcache) Read(key string) (interface{}, error) {
item, err := m.client.Get(key)
if err != nil {
if err == memcache.ErrCacheMiss {
return nil, ErrKeyNotExist
}
return nil, err
}
return item.Value, nil
}
// Delete deletes content of the given key from in memcached storage
func (m Memcache) Delete(key string) error {
return m.client.Delete(key)
}
// Flush flushes memcached storage
func (m Memcache) Flush() error {
return m.client.DeleteAll()
}