-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.go
More file actions
66 lines (57 loc) · 1.36 KB
/
redis.go
File metadata and controls
66 lines (57 loc) · 1.36 KB
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
/**
* Copyright 2019 Innodev LLC. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package cache
import (
"time"
"github.com/go-redis/cache/v7"
"github.com/go-redis/redis/v7"
"github.com/vmihailenco/msgpack/v4"
)
type cacheRedis struct {
prefix string
exp time.Duration
codec *cache.Codec
}
type RedisConfig struct {
Servers map[string]string
Password string
Database int
Prefix string
Expiration time.Duration
}
func NewRedis(conf RedisConfig) Cache {
return &cacheRedis{
codec: &cache.Codec{
Redis: redis.NewRing(&redis.RingOptions{
Addrs: conf.Servers,
Password: conf.Password,
DB: conf.Database,
}),
Marshal: func(v interface{}) ([]byte, error) {
return msgpack.Marshal(v)
},
Unmarshal: func(b []byte, v interface{}) error {
return msgpack.Unmarshal(b, v)
},
},
exp: conf.Expiration,
prefix: conf.Prefix,
}
}
func (c *cacheRedis) Add(key string, value interface{}) error {
return c.codec.Set(&cache.Item{
Key: c.prefix + key,
Object: value,
Expiration: c.exp,
})
}
func (c *cacheRedis) Get(key string, out interface{}) (bool, error) {
err := c.codec.Get(c.prefix+key, out)
return err == nil, err
}
func (c *cacheRedis) Remove(key string) error {
return c.codec.Delete(c.prefix + key)
}