/
redis_client.go
60 lines (44 loc) · 1.17 KB
/
redis_client.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
package redis
import (
"fmt"
"github.com/go-redis/redis"
)
type RedisClient struct {
client *redis.Client
}
type RedisClientOption struct {
Addr string
Password string
}
func NewRedisClient(options RedisClientOption) *RedisClient {
client := redis.NewClient(&redis.Options{Addr: options.Addr, Password: options.Password})
return &RedisClient{client: client}
}
func (c *RedisClient) LPush(key string, values []byte) error {
if err := c.client.LPush(key, values).Err(); err != nil {
return fmt.Errorf("redis lpush: %w", err)
}
return nil
}
func (c *RedisClient) SAdd(key string, values []byte) error {
if err := c.client.SAdd(key, values).Err(); err != nil {
return fmt.Errorf("redis sadd: %w", err)
}
return nil
}
func (c *RedisClient) LRange(key string, start, stop int64) ([]string, error) {
cmd := c.client.LRange(key, start, stop)
res, err := cmd.Result()
if err != nil {
return nil, fmt.Errorf("redis lrange: %w", err)
}
return res, nil
}
func (c *RedisClient) SMembers(key string) ([]string, error) {
cmd := c.client.SMembers(key)
res, err := cmd.Result()
if err != nil {
return nil, fmt.Errorf("redis lrange: %w", err)
}
return res, nil
}