-
Notifications
You must be signed in to change notification settings - Fork 5
/
buckets_redigo.go
112 lines (94 loc) · 2.26 KB
/
buckets_redigo.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
package rerate
import (
"strconv"
"time"
"github.com/garyburd/redigo/redis"
)
// RedigoBuckets is a Buckets using redigo as backend
type RedigoBuckets struct {
pool *redis.Pool
size int64
ttl time.Duration
}
// NewRedigoBuckets is a RedigoBuckets factory
func NewRedigoBuckets(redis *redis.Pool) BucketsFactory {
return func(size int64, ttl time.Duration) Buckets {
return &RedigoBuckets{
pool: redis,
size: size,
ttl: ttl,
}
}
}
// cleanup unused bucket(s)
func (bs *RedigoBuckets) cleanup(key string, from int64) {
conn := bs.pool.Get()
defer conn.Close()
if l, err := redis.Int64(conn.Do("HLEN", key)); err != nil || l < bs.size*2 {
return
}
if ids, err := redis.Strings(conn.Do("HKEYS", key)); err == nil {
var delIds []int64
for _, s := range ids {
if v, err := strconv.ParseInt(s, 10, 64); err == nil && v <= from-bs.size {
delIds = append(delIds, v)
}
}
bs.Del(key, delIds...)
}
}
// Inc increment bucket key:id 's occurs
func (bs *RedigoBuckets) Inc(key string, id int64) error {
conn := bs.pool.Get()
defer conn.Close()
conn.Send("MULTI")
conn.Send("HINCRBY", key, strconv.FormatInt(id, 10), 1)
conn.Send("PEXPIRE", key, int64(bs.ttl/time.Millisecond))
ret, err := redis.Ints(conn.Do("EXEC"))
if err != nil {
return err
}
if ret[0] == 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 *RedigoBuckets) Del(key string, ids ...int64) error {
conn := bs.pool.Get()
defer conn.Close()
if len(ids) == 0 {
_, err := conn.Do("DEL", key)
return err
}
args := make([]interface{}, len(ids)+1)
args[0] = key
for i, v := range ids {
args[i+1] = v
}
_, err := conn.Do("HDEL", args...)
return err
}
// Get return bucket key:ids' occurs
func (bs *RedigoBuckets) Get(key string, ids ...int64) ([]int64, error) {
args := make([]interface{}, len(ids)+1)
args[0] = key
for i, v := range ids {
args[i+1] = v
}
conn := bs.pool.Get()
defer conn.Close()
vals, err := redis.Strings(conn.Do("HMGET", args...))
if err != nil {
return []int64{}, err
}
ret := make([]int64, len(ids))
for i, val := range vals {
if v, e := strconv.ParseInt(val, 10, 64); e == nil {
ret[i] = v
} else {
ret[i] = 0
}
}
return ret, nil
}