-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisson.go
97 lines (86 loc) · 1.89 KB
/
redisson.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package redisson
import (
"context"
"errors"
"github.com/coreos/go-semver/semver"
"github.com/redis/rueidis"
"github.com/redis/rueidis/rueidiscompat"
"sync"
"time"
)
type RESP = string
const (
RESP2 RESP = "RESP2"
RESP3 RESP = "RESP3"
)
var Nil = rueidis.Nil
func IsNil(err error) bool { return errors.Is(err, Nil) }
type client struct {
v ConfInterface
version semver.Version
handler handler
isCluster bool
cmd rueidis.Client
adapter rueidiscompat.Cmdable
ttl time.Duration
builder builder
maxp int
delayQueues sync.Map
once sync.Once
}
func MustNewClient(v ConfInterface) Cmdable {
cmd, err := Connect(v)
if err != nil {
panic(err)
}
return cmd
}
func (c *client) Options() ConfVisitor { return c.v }
func (c *client) IsCluster() bool { return c.isCluster }
func (c *client) ForEachNodes(ctx context.Context, f func(context.Context, Cmdable) error) error {
if !c.isCluster {
return f(ctx, c)
}
var errs Errors
for _, v := range c.cmd.Nodes() {
err := f(ctx, &client{
v: c.v,
version: c.version,
handler: c.handler,
isCluster: c.isCluster,
cmd: v,
adapter: rueidiscompat.NewAdapter(v),
builder: c.builder,
maxp: c.maxp,
})
if err != nil {
errs.Push(err)
}
}
return errs.Err()
}
func (c *client) Cache(ttl time.Duration) CacheCmdable {
if !c.v.GetEnableCache() || c.ttl == ttl {
return c
}
cp := &client{
v: c.v,
version: c.version,
handler: c.handler,
isCluster: c.isCluster,
cmd: c.cmd,
adapter: c.adapter,
ttl: ttl,
builder: c.builder,
maxp: c.maxp,
}
return cp
}
func (c *client) Do(ctx context.Context, completed Completed) RedisResult {
if c.ttl <= 0 {
return c.cmd.Do(ctx, completed)
}
resp := c.cmd.DoCache(ctx, rueidis.Cacheable(completed), c.ttl)
c.handler.cache(ctx, resp.IsCacheHit())
return resp
}