-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrede.go
127 lines (115 loc) · 3.32 KB
/
rede.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package go_rede
import (
"context"
"github.com/go-redis/redis/v8"
"math"
"time"
)
// Client is a Redis client representing a pool of zero or more
// underlying connections. It's safe for concurrent use by multiple
// goroutines.
type Client struct {
Namespaces string
*redis.Client
}
func NewClient(opt *Options) *Client {
redisOpt := &redis.Options{
Network: opt.Network,
Addr: opt.Addr,
Password: opt.Password,
DB: opt.DB,
MaxRetries: opt.MaxRetries,
MinRetryBackoff: opt.MinRetryBackoff,
MaxRetryBackoff: opt.MaxRetryBackoff,
DialTimeout: opt.DialTimeout,
ReadTimeout: opt.ReadTimeout,
WriteTimeout: opt.WriteTimeout,
PoolSize: opt.PoolSize,
MinIdleConns: opt.MinIdleConns,
MaxConnAge: opt.MaxConnAge,
PoolTimeout: opt.PoolTimeout,
IdleTimeout: opt.IdleTimeout,
IdleCheckFrequency: opt.IdleCheckFrequency,
TLSConfig: opt.TLSConfig,
Limiter: opt.Limiter,
}
return &Client{
Namespaces: opt.Namespaces,
Client: redis.NewClient(redisOpt),
}
}
// Push an Member into the Rede for ttl.Seconds() seconds
func (c *Client) Push(ctx context.Context, member string, ttl time.Duration) (int64, error) {
z := &redis.Z{
Score: float64(time.Now().Unix()) + ttl.Seconds(),
Member: member,
}
result := c.ZAdd(ctx, c.Namespaces, z)
return result.Result()
}
// Pull the members, remove it from the rede before it expires.
func (c *Client) Pull(ctx context.Context, members ...string) (int64, error) {
items := make([]interface{}, len(members))
for i, member := range members {
items[i] = member
}
result := c.ZRem(ctx, c.Namespaces, items...)
return result.Result()
}
// Look Show the ttl corresponding with element and without removing it from the rede.
func (c *Client) Look(ctx context.Context, member string) (float64, error) {
result, err := c.ZScore(ctx, c.Namespaces, member).Result()
if err != nil {
return 0, err
}
return math.Max(result-float64(time.Now().Unix()), 0), nil
}
// Ttn Show the time left (in seconds) until the next element will expire.
func (c *Client) Ttn(ctx context.Context) (float64, error) {
result, err := c.ZRangeWithScores(ctx, c.Namespaces, 0, 0).Result()
if len(result) == 0 {
return -1, nil
}
return math.Max(0, result[0].Score-float64(time.Now().Unix())), err
}
//Poll return all the expired members in rede.
// cur := c.Poll(ctx)
// for cur.Next() {
// member, err := cur.Get()
// fmt.Println(member, err)
// }
func (c *Client) Poll(ctx context.Context) *pollCursor {
return newPollCursor(ctx, c)
}
type pollCursor struct {
ctx context.Context
c *Client
value string
err error
}
func newPollCursor(ctx context.Context, c *Client) *pollCursor {
return &pollCursor{ctx: ctx, c: c}
}
//Next get the next element in the form of iteration
func (pc *pollCursor) Next() bool {
if pc.err != nil {
return false
}
zSlice, err := pc.c.ZPopMin(pc.ctx, pc.c.Namespaces, 1).Result()
if err != nil {
pc.value, pc.err = "", err
return true
}
if len(zSlice) == 0 {
return false
}
if zSlice[0].Score > float64(time.Now().Unix()) {
pc.c.ZAdd(pc.ctx, pc.c.Namespaces, &zSlice[0])
return false
}
pc.value = zSlice[0].Member.(string)
return true
}
func (pc *pollCursor) Get() (string, error) {
return pc.value, pc.err
}