-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
63 lines (51 loc) · 1.06 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
package infrastructure
import (
"time"
"github.com/go-redis/redis"
"github.com/scarlet0725/prism-api/infrastructure/repository"
"github.com/scarlet0725/prism-api/model"
)
type redisCache struct {
Client *redis.Client
}
func NewRedisManager(c *redis.Client) repository.Cache {
client := &redisCache{
Client: c,
}
return client
}
func (c *redisCache) Set(d *model.CacheData, ttl int64) error {
cmd := c.Client.Set(d.Key, d.Value, time.Duration(ttl)*time.Second)
err := cmd.Err()
return err
}
func (c *redisCache) Get(d *model.CacheData) (*model.CacheData, error) {
cmd := c.Client.Get(d.Key)
err := cmd.Err()
if err != nil {
return nil, err
}
data, err := cmd.Bytes()
if err != nil {
return nil, err
}
return &model.CacheData{
Key: d.Key,
Value: data,
}, nil
}
func (c *redisCache) GetByKey(key string) (*model.CacheData, error) {
cmd := c.Client.Get(key)
err := cmd.Err()
if err != nil {
return nil, err
}
data, err := cmd.Bytes()
if err != nil {
return nil, err
}
return &model.CacheData{
Key: key,
Value: data,
}, nil
}