Newer
Older
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
package redis
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"code.vereign.com/gaiax/tsa/golib/errors"
)
type Client struct {
rdb *redis.Client
defaultTTL time.Duration
}
func New(addr, user, pass string, db int, defaultTTL time.Duration) *Client {
rdb := redis.NewClient(&redis.Options{
Addr: addr,
Username: user,
Password: pass,
DB: db,
DialTimeout: 10 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
})
return &Client{
rdb: rdb,
}
}
func (c *Client) Get(ctx context.Context, key string) ([]byte, error) {
result := c.rdb.Get(ctx, key)
if result.Err() != nil {
if result.Err() == redis.Nil {
return nil, errors.New(errors.NotFound)
}
return nil, result.Err()
}
return []byte(result.Val()), nil
}