-
Notifications
You must be signed in to change notification settings - Fork 5
/
buckets_redisv5.go
102 lines (86 loc) · 2.13 KB
/
buckets_redisv5.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
package rerate
import (
"strconv"
"time"
redis "gopkg.in/redis.v5"
)
// RedisV5Buckets is a Buckets using redis.v5 as backend
type RedisV5Buckets struct {
redis *redis.Client
size int64
ttl time.Duration
}
// NewRedisV5Buckets is a RedisV5Buckets factory
func NewRedisV5Buckets(redis *redis.Client) BucketsFactory {
return func(size int64, ttl time.Duration) Buckets {
return &RedisV5Buckets{
redis: redis,
size: size,
ttl: ttl,
}
}
}
func (bs *RedisV5Buckets) cleanup(key string, from int64) {
if l, err := bs.redis.HLen(key).Result(); err != nil || l < bs.size*2 {
return
}
if ids, err := bs.redis.HKeys(key).Result(); err == nil {
var delIds []int64
for _, s := range ids {
if v, e := strconv.ParseInt(s, 10, 64); e == nil && v <= from-bs.size {
delIds = append(delIds, v)
}
}
bs.Del(key, delIds...)
}
}
// Inc increment bucket key:id 's occurs
func (bs *RedisV5Buckets) Inc(key string, id int64) error {
pipe := bs.redis.TxPipeline()
defer pipe.Close()
count := pipe.HIncrBy(key, strconv.FormatInt(id, 10), 1)
pipe.PExpire(key, bs.ttl)
_, err := pipe.Exec()
if err != nil {
return err
}
if count.Val() == 1 { // new bucket created
go bs.cleanup(key, id)
}
return nil
}
// Del delete bucket key:ids, or delete Buckets key when ids is empty.
func (bs *RedisV5Buckets) Del(key string, ids ...int64) error {
if len(ids) == 0 {
_, err := bs.redis.Del(key).Result()
return err
}
args := make([]string, len(ids))
for i, v := range ids {
args[i] = strconv.FormatInt(v, 10)
}
_, err := bs.redis.HDel(key, args...).Result()
return err
}
// Get return bucket key:ids' occurs
func (bs *RedisV5Buckets) Get(key string, ids ...int64) ([]int64, error) {
args := make([]string, len(ids))
for i, v := range ids {
args[i] = strconv.FormatInt(v, 10)
}
results, err := bs.redis.HMGet(key, args...).Result()
if err != nil {
return []int64{}, err
}
vals := make([]int64, len(ids))
for i, result := range results {
if result == nil {
vals[i] = 0
} else if v, e := strconv.ParseInt(result.(string), 10, 64); e != nil {
vals[i] = 0
} else {
vals[i] = v
}
}
return vals, nil
}