-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
106 lines (94 loc) · 2.02 KB
/
redis.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
package bitmap
import (
"context"
"github.com/go-redis/redis/v8"
"time"
)
type RedisOption func(*Redis) error
type Redis struct {
ctx context.Context
client *redis.Client
key string
m uint64
}
func (r *Redis) CheckBits(locs []uint64) (bool, error) {
pl := r.client.Pipeline()
var results []*redis.IntCmd
for _, loc := range locs {
results = append(results, pl.GetBit(r.ctx, r.key, int64(loc%r.m)))
}
_, err := pl.Exec(r.ctx)
if err != nil {
return false, err
}
for _, v := range results {
res, err := v.Result()
if err != nil {
return false, err
}
if res == 0 {
return false, nil
}
}
return true, nil
}
func (r *Redis) SetBits(locs []uint64) error {
pl := r.client.Pipeline()
var results []*redis.IntCmd
for _, loc := range locs {
results = append(results, pl.SetBit(r.ctx, r.key, int64(loc%r.m), 1))
}
_, err := pl.Exec(r.ctx)
if err != nil {
return err
}
for _, v := range results {
_, err := v.Result()
if err != nil {
return err
}
}
return nil
}
// RedisSetExpireTTL sets expiry TTL with d.
func RedisSetExpireTTL(d time.Duration) RedisOption {
return func(r *Redis) error {
res := r.client.Expire(r.ctx, r.key, d)
_, err := res.Result()
if err != nil {
return err
}
return nil
}
}
func (r *Redis) setEmptyBitmap() error {
res, err := r.client.Keys(r.ctx, r.key).Result()
if err != nil {
return err
}
if len(res) == 0 {
r.client.SetBit(r.ctx, r.key, 0, 0)
}
return nil
}
// NewRedis returns bitmap that is store into redis and manipulated via github.com/go-redis/redis.
func NewRedis(ctx context.Context, client *redis.Client, key string, m uint64, opts ...RedisOption) (*Redis, error) {
r := &Redis{
ctx: ctx,
client: client,
key: key,
m: m,
}
// Set the empty bitmap with the r.key to avoid subsequent RedisOption might not be effective such as RedisSetExpireTTL.
err := r.setEmptyBitmap()
if err != nil {
return nil, err
}
for _, opt := range opts {
err := opt(r)
if err != nil {
return nil, err
}
}
return r, nil
}