-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycle.go
48 lines (37 loc) · 948 Bytes
/
cycle.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
package kredis
import "github.com/redis/go-redis/v9"
type Cycle struct {
Proxy
values []string
}
func NewCycle(key string, values []string, opts ...ProxyOption) (*Cycle, error) {
proxy, err := NewProxy(key, opts...)
if err != nil {
return nil, err
}
return &Cycle{Proxy: *proxy, values: values}, nil
}
func (c *Cycle) Index() (idx int64) {
idx, err := c.client.Get(c.ctx, c.key).Int64()
if err != nil && err != redis.Nil {
if debugLogger != nil {
debugLogger.Warn("Cycle#Index", err)
}
}
return
}
func (c *Cycle) IndexResult() (int64, error) {
return c.client.Get(c.ctx, c.key).Int64()
}
func (c *Cycle) Value() string {
return c.values[c.Index()]
}
func (c *Cycle) Next() (err error) {
idx, err := c.client.Get(c.ctx, c.key).Int64()
if err != nil && err != redis.Nil {
return // GET error
}
value := (idx + 1) % int64(len(c.values))
_, err = c.client.Set(c.ctx, c.key, value, c.expiresIn).Result()
return
}