This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrediswrapper.go
67 lines (53 loc) · 1.78 KB
/
rediswrapper.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
package main
import (
"time"
"github.com/go-redis/redis"
)
// mock our use of redis pubsub for modularity/testing purposes
type PubSubber interface {
Subscribe(channels ...string) error
Unsubscribe(channels ...string) error
}
// mock our use of redis client for modularity/testing purposes
type RedisClient interface {
ZAdd(key string, members ...redis.Z) (int64, error)
ZCount(key, min, max string) (int64, error)
Expire(key string, dur time.Duration) (bool, error)
Publish(channel, message string) (int64, error)
ZRevRange(key, start, stop string) ([]string, error)
Exists(key string) (bool, error)
Get(key string) (string, error)
Incr(key string) (int64, error)
}
// wraps redis.Client as a RedisClient above, which gives a simpler interface
// for basic usage and mocking
type redisWrapper struct {
r *redis.Client
}
func NewRedisWrapper(r *redis.Client) RedisClient {
return &redisWrapper{r}
}
func (rw *redisWrapper) ZAdd(key string, members ...redis.Z) (int64, error) {
return rw.r.ZAdd(key, members...).Result()
}
func (rw *redisWrapper) ZCount(key, min, max string) (int64, error) {
return rw.r.ZCount(key, min, max).Result()
}
func (rw *redisWrapper) Expire(key string, dur time.Duration) (bool, error) {
return rw.r.Expire(key, dur).Result()
}
func (rw *redisWrapper) Publish(channel, message string) (int64, error) {
return rw.r.Publish(channel, message).Result()
}
func (rw *redisWrapper) ZRevRange(key, start, stop string) ([]string, error) {
return rw.r.ZRevRange(key, start, stop).Result()
}
func (rw *redisWrapper) Exists(key string) (bool, error) {
return rw.r.Exists(key).Result()
}
func (rw *redisWrapper) Get(key string) (string, error) {
return rw.r.Get(key).Result()
}
func (rw *redisWrapper) Incr(key string) (int64, error) {
return rw.r.Incr(key).Result()
}