-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
83 lines (72 loc) · 1.89 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
package recache
import (
"time"
"github.com/go-redis/redis"
"github.com/pkg/errors"
)
const tagPrefix = "tag"
// ErrKeyNotFound says that `key doesn't exist`
var ErrKeyNotFound = errors.New("key doesn't exist")
// RedisCache implements cache for redis
type RedisCache struct {
redis *redis.Client
}
// NewRedisCache returns new RedisCache
func NewRedisCache(redis *redis.Client) RedisCache {
return RedisCache{
redis: redis,
}
}
// Set saves data into redis
// Use ttl for `SETEX`-like behavior.
// Zero expiration means the key has no expiration time.
func (m *RedisCache) Set(id string, data interface{}, ttl uint, tags ...string) error {
err := m.redis.Set(id, data, time.Duration(ttl)*time.Second).Err()
if err != nil {
return errors.Wrap(err, "can't set value")
}
if len(tags) == 0 {
return nil
}
return errors.Wrap(m.setTags(id, tags...), "can't set tags")
}
// Get returns data from redis by key
func (m *RedisCache) Get(id string) ([]byte, error) {
result, err := m.redis.Get(id).Bytes()
if err == redis.Nil {
return nil, ErrKeyNotFound
}
return result, errors.Wrap(err, "can't get value")
}
// ClearByTag deletes all keys that have the specified tag
func (m *RedisCache) ClearByTag(tag string) error {
tag = m.prefixTag(tag)
var cursor uint64
for {
keys, cursor, err := m.redis.SScan(tag, cursor, "", 0).Result()
if err != nil {
return errors.Wrap(err, "can't sscan")
}
pipe := m.redis.Pipeline()
pipe.Del(tag)
pipe.Del(keys...)
if _, err = pipe.Exec(); err != nil {
return errors.Wrap(err, "can't exec")
}
if cursor == 0 {
return nil
}
}
}
func (m *RedisCache) setTags(id string, tags ...string) error {
for _, tag := range tags {
tag = m.prefixTag(tag)
if err := m.redis.SAdd(tag, id).Err(); err != nil {
return errors.Wrap(err, "can't add tag")
}
}
return nil
}
func (m *RedisCache) prefixTag(tag string) string {
return tagPrefix + ":" + tag
}