-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis.go
62 lines (49 loc) · 1.24 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
package vcache
import (
"sync"
"time"
"github.com/coseyo/radixpool"
)
const (
radixErrEmpty = "string value is not available for this reply type"
)
var (
RedisPool *radixpool.Pool
doOnce sync.Once
)
// init redis config
func InitRedis(network, addr string, size int, clientTimeout time.Duration, password string, dbNum int) error {
var err error
doOnce.Do(func() {
RedisPool, err = radixpool.NewPool(network, addr, size, clientTimeout, password, dbNum)
})
return err
}
func get(key string) (str string, err error) {
str, err = RedisPool.Cmd("GET", key).Str()
if err != nil && err.Error() == radixErrEmpty {
err = nil
}
return
}
func set(key, value string) error {
return RedisPool.Cmd("SET", key, value).Err
}
func setex(key string, seconds int, value string) error {
return RedisPool.Cmd("SETEX", key, seconds, value).Err
}
func del(key string) error {
return RedisPool.Cmd("DEL", key).Err
}
func expire(key string, seconds int) error {
return RedisPool.Cmd("EXPIRE", key, seconds).Err
}
func incr(key string) (int, error) {
return RedisPool.Cmd("INCR", key).Int()
}
func decr(key string) (int, error) {
return RedisPool.Cmd("DECR", key).Int()
}
func ttl(key string) (int, error) {
return RedisPool.Cmd("TTl", key).Int()
}