-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathredis.go
83 lines (63 loc) · 1.79 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package redis
import (
"context"
"github.com/go-redis/redis/v8"
"time"
)
type Client struct {
redisPool *redis.Client
}
func NewRedisClient(host string, port string, password string, db int, poolSize int) Client {
client := Client{
redisPool: redis.NewClient(&redis.Options{
Addr: host + ":" + port,
Password: password,
DB: db,
PoolSize: poolSize,
PoolTimeout: 60 * time.Second,
}),
}
err := client.Ping()
if err != nil {
panic("YTask: connect redisBroker error : " + err.Error())
}
return client
}
// =======================
// high api
// =======================
func (c *Client) Exists(key string) (bool, error) {
r, err := c.redisPool.Exists(context.Background(), key).Result()
return r == 1, err
}
func (c *Client) Get(key string) *redis.StringCmd {
return c.redisPool.Get(context.Background(), key)
}
func (c *Client) Set(key string, value interface{}, exTime time.Duration) error {
if exTime <= 0 {
exTime = 0
}
return c.redisPool.Set(context.Background(), key, value, exTime).Err()
}
func (c *Client) RPush(key string, value interface{}) error {
return c.redisPool.RPush(context.Background(), key, value).Err()
}
func (c *Client) LPush(key string, value interface{}) error {
return c.redisPool.LPush(context.Background(), key, value).Err()
}
func (c *Client) BLPop(key string, timeout time.Duration) *redis.StringSliceCmd {
return c.redisPool.BLPop(context.Background(), timeout, key)
}
func (c *Client) Do(args ...interface{}) *redis.Cmd {
var ctx = context.Background()
return c.redisPool.Do(ctx, args)
}
func (c *Client) Flush() error {
return c.redisPool.FlushDB(context.Background()).Err()
}
func (c *Client) Ping() error {
return c.redisPool.Ping(context.Background()).Err()
}
func (c *Client) Close() {
c.redisPool.Close()
}