-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
46 lines (38 loc) · 841 Bytes
/
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
package cache
import (
"encoding/json"
"time"
redisClient "github.com/go-redis/redis"
)
type redis struct {
client *redisClient.Client
}
func Redis(options *redisClient.Options) Storage {
clnt := redisClient.NewClient(options)
return redis{
client: clnt,
}
}
func (r redis) Write(key string, v interface{}, expiration time.Duration) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
return r.client.Set(key, string(b), expiration).Err()
}
func (r redis) Read(key string) (interface{}, error) {
result := r.client.Get(key)
if result.Err() != nil {
if result.Err() == redisClient.Nil {
return nil, ErrKeyNotExist
}
return nil, result.Err()
}
return result.Result()
}
func (r redis) Delete(key string) error {
return r.client.Del(key).Err()
}
func (r redis) Flush() error {
return r.Flush()
}