-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.go
More file actions
82 lines (73 loc) · 1.69 KB
/
redis.go
File metadata and controls
82 lines (73 loc) · 1.69 KB
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
package hybrid
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/garyburd/redigo/redis"
)
type redisCache[T any] struct {
conn redis.Conn
ttl time.Duration
opt *Option
}
func (r *redisCache[T]) Set(ctx context.Context, key string, val T, isEmpty bool) error {
ttl := r.ttl
if isEmpty {
if !r.opt.Empty {
return nil
}
if r.opt.EmptyTtl > 0 {
ttl = r.opt.EmptyTtl
}
}
w := NewWarp(val)
bytes, err := json.Marshal(w)
if err != nil {
return fmt.Errorf("val serialize error:%w, [%T]%+v", err, val, val)
}
if _, err = r.conn.Do("SET", r.key(key), bytes, "EX", int(ttl/time.Second)); err != nil {
return fmt.Errorf("redis set error:%w", err)
}
return nil
}
func (r *redisCache[T]) Get(ctx context.Context, key string) (T, error) {
data, err := redis.Bytes(r.conn.Do("GET", r.key(key)))
if errors.Is(err, redis.ErrNil) {
return *new(T), NotFindCache
}
if err != nil {
return *new(T), fmt.Errorf("redis get error:%w", err)
}
w := NewWarpEmpty[T]()
err = json.Unmarshal(data, w)
if err != nil {
return *new(T), fmt.Errorf("json unmarshal error:%w", err)
}
return w.Value, nil
}
func (r *redisCache[T]) Del(ctx context.Context, key string) error {
if _, err := r.conn.Do("DEL", r.key(key)); err != nil {
return fmt.Errorf("redis del error:%w", err)
}
return nil
}
func (r *redisCache[T]) key(key string) string {
if r.opt.Prefix != "" {
return fmt.Sprintf("[%s]%s", r.opt.Prefix, key)
}
return key
}
// WithRedis 使用redis缓存
func WithRedis[T any](conn redis.Conn, ttl time.Duration, opts ...Options) Cache[T] {
opt := &Option{}
for _, optFn := range opts {
optFn(opt)
}
return &redisCache[T]{
conn: conn,
ttl: ttl,
opt: opt,
}
}