-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
59 lines (49 loc) · 1.23 KB
/
redis.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
package cache
import (
"encoding/json"
"time"
"gopkg.in/redis.v3"
)
const redisStoreName = "Redis Store"
// NewRedisStore creates a new redis store using the given client
func NewRedisStore(client *redis.Client) Store {
return &redisstore{
client: client,
config: StoreConfig{StoreName: redisStoreName},
}
}
type redisstore struct {
client *redis.Client
config StoreConfig
}
func (rs *redisstore) Store(cacheKey string, data []byte) error {
bytes, err := json.Marshal(cacheddata{data, time.Now()})
if err != nil {
return err
}
return rs.client.Set(cacheKey, bytes, 0).Err()
}
func (rs *redisstore) Retrieve(cacheKey string) (*cacheddata, bool, error) {
cachedBytes, err := rs.client.Get(cacheKey).Bytes()
if err != nil && err != redis.Nil {
return nil, false, err
}
if err == redis.Nil {
return nil, false, nil
}
cachedData := &cacheddata{}
if err := json.Unmarshal(cachedBytes, cachedData); err != nil {
return nil, false, err
}
return cachedData, true, nil
}
func (rs *redisstore) Delete(cacheKey string) error {
_, err := rs.client.Del(cacheKey).Result()
return err
}
func (rs *redisstore) Config() StoreConfig {
return rs.config
}
func (rs *redisstore) SetConfig(config StoreConfig) {
rs.config = config
}