forked from adjust/rmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_wrapper.go
111 lines (95 loc) · 2.56 KB
/
redis_wrapper.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package rmq
import (
"log"
"time"
"github.com/go-redis/redis"
)
type RedisWrapper struct {
rawClient *redis.Client
scripts map[string]string
}
func (wrapper RedisWrapper) GetClient() *redis.Client {
return wrapper.rawClient
}
func (wrapper RedisWrapper) Set(key string, value interface{}, expiration time.Duration) bool {
return checkErr(wrapper.rawClient.Set(key, value, expiration).Err())
}
func (wrapper RedisWrapper) Get(key string) (error, interface{}) {
res, err := wrapper.rawClient.Get(key).Result()
if err != nil {
return err, ""
}
return nil, res
}
func (wrapper RedisWrapper) Del(key string) (affected int, ok bool) {
n, err := wrapper.rawClient.Del(key).Result()
ok = checkErr(err)
if !ok {
return 0, false
}
return int(n), ok
}
func (wrapper RedisWrapper) TTL(key string) (ttl time.Duration, ok bool) {
ttl, err := wrapper.rawClient.TTL(key).Result()
ok = checkErr(err)
if !ok {
return 0, false
}
return ttl, ok
}
func (wrapper RedisWrapper) LLen(key string) (affected int, ok bool) {
n, err := wrapper.rawClient.LLen(key).Result()
ok = checkErr(err)
if !ok {
return 0, false
}
return int(n), ok
}
func (wrapper RedisWrapper) SAdd(key, value string) bool {
return checkErr(wrapper.rawClient.SAdd(key, value).Err())
}
func (wrapper RedisWrapper) SMembers(key string) []string {
members, err := wrapper.rawClient.SMembers(key).Result()
if ok := checkErr(err); !ok {
return []string{}
}
return members
}
func (wrapper RedisWrapper) SRem(key, value string) (affected int, ok bool) {
n, err := wrapper.rawClient.SRem(key, value).Result()
ok = checkErr(err)
if !ok {
return 0, false
}
return int(n), ok
}
func (wrapper RedisWrapper) SCount(key string) (affected int, ok bool) {
n, err := wrapper.rawClient.ZCount(key, "-inf", "+inf").Result()
ok = checkErr(err)
if !ok {
return 0, false
}
return int(n), ok
}
func (wrapper RedisWrapper) FlushDb() {
wrapper.rawClient.FlushDB()
}
func (wrapper RedisWrapper) RunShaScript(shaScriptKey string, keys []string, args ...interface{}) *redis.Cmd {
cmd := wrapper.rawClient.EvalSha(wrapper.scripts[shaScriptKey], keys, args...)
if cmd.Err() != nil && cmd.Err() != redis.Nil {
log.Println("RunShaScript Error: operation=" + shaScriptKey + " message=" + cmd.Err().Error())
}
return cmd
}
// checkErr returns true if there is no error, false if the result error is nil and panics if there's another error
func checkErr(err error) (ok bool) {
switch err {
case nil:
return true
case redis.Nil:
return false
default:
log.Panicf("rmq redis error is not nil %s", err)
return false
}
}