-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
141 lines (121 loc) · 3.36 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
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
package cachier
import (
"strings"
"time"
"github.com/go-redis/redis"
)
// Logger is interface for logging
type Logger interface {
Error(...interface{})
Warn(...interface{})
Print(...interface{})
}
// DummyLogger is implementation of Logger that does not log anything
type DummyLogger struct{}
// Error does nothing
func (d DummyLogger) Error(...interface{}) {}
// Warn does nothing
func (d DummyLogger) Warn(...interface{}) {}
// Print does nothing
func (d DummyLogger) Print(...interface{}) {}
//
// RedisCache implements cachier.CacheTTL interface using redis storage
type RedisCache struct {
redisClient *redis.Client
keyPrefix string
marshal func(value interface{}) ([]byte, error)
unmarshal func(b []byte, value *interface{}) error
ttl time.Duration
logger Logger
}
// NewRedisCache is a constructor that creates a RedisCache
func NewRedisCache(
redisClient *redis.Client,
keyPrefix string,
marshal func(value interface{}) ([]byte, error),
unmarshal func(b []byte, value *interface{}) error,
ttl time.Duration,
) *RedisCache {
return &RedisCache{
redisClient: redisClient,
keyPrefix: keyPrefix,
marshal: marshal,
unmarshal: unmarshal,
ttl: ttl,
logger: DummyLogger{},
}
}
// NewRedisCacheWithLogger is a constructor that creates a RedisCache
func NewRedisCacheWithLogger(
redisClient *redis.Client,
keyPrefix string,
marshal func(value interface{}) ([]byte, error),
unmarshal func(b []byte, value *interface{}) error,
ttl time.Duration,
logger Logger,
) *RedisCache {
return &RedisCache{
redisClient: redisClient,
keyPrefix: keyPrefix,
marshal: marshal,
unmarshal: unmarshal,
ttl: ttl,
logger: logger,
}
}
// Get gets a cached value by key
func (rc *RedisCache) Get(key string) (interface{}, error) {
rc.logger.Print("redis get " + rc.keyPrefix + key)
value, err := rc.redisClient.Get(rc.keyPrefix + key).Result()
if err == redis.Nil {
return nil, ErrNotFound
} else if err != nil {
return nil, err
}
var result interface{}
rc.unmarshal([]byte(value), &result)
return result, nil
}
// Peek gets a cached value by key without any sideeffects (identical as Get in this implementation)
func (rc *RedisCache) Peek(key string) (interface{}, error) {
return rc.Get(key)
}
// Set stores a key-value pair into cache
func (rc *RedisCache) Set(key string, value interface{}) error {
marshalledValue, err := rc.marshal(value)
if err != nil {
return err
}
rc.logger.Print("redis set " + rc.keyPrefix + key)
return rc.redisClient.Set(rc.keyPrefix+key, marshalledValue, rc.ttl).Err()
}
// Delete removes a key from cache
func (rc *RedisCache) Delete(key string) error {
return rc.redisClient.Del(rc.keyPrefix + key).Err()
}
// Keys returns all the keys in the cache
func (rc *RedisCache) Keys() ([]string, error) {
keys, err := rc.redisClient.Keys(rc.keyPrefix + "*").Result()
if err != nil {
return nil, err
}
strippedKeys := make([]string, 0, len(keys))
for _, key := range keys {
strippedKeys = append(strippedKeys, strings.TrimPrefix(key, rc.keyPrefix))
}
return strippedKeys, nil
}
// Purge removes all the records from the cache
func (rc *RedisCache) Purge() error {
//FIXME: delete all keys from redis at once
keys, err := rc.Keys()
if err != nil {
return err
}
for _, key := range keys {
if err := rc.Delete(key); err != nil {
return err
}
}
return nil
}