-
Notifications
You must be signed in to change notification settings - Fork 4
/
db-redis-sortedset.go
69 lines (60 loc) · 1.33 KB
/
db-redis-sortedset.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
package Figo
import (
"github.com/garyburd/redigo/redis"
)
type RedisSortedSet struct {
key string
rp *redis.Pool
}
func NewRedisSortedSet(key string, rp *redis.Pool) *RedisSortedSet {
return &RedisSortedSet{
key: key,
rp: rp,
}
}
func (p *RedisSortedSet) ZAdd(score int64, name string) {
c := p.rp.Get()
defer c.Close()
c.Do("ZADD", p.key, score, name)
}
func (p *RedisSortedSet) ZBatchAdd(ssitems ...SSItem) {
c := p.rp.Get()
defer c.Close()
for _, ssitem := range ssitems {
c.Send("ZADD", p.key, ssitem.Score, ssitem.Key)
}
c.Flush()
}
func (p *RedisSortedSet) ZRem(name string) {
c := p.rp.Get()
defer c.Close()
c.Do("ZREM", p.key, name)
}
func (p *RedisSortedSet) ZScore(name string) int64 {
c := p.rp.Get()
defer c.Close()
if score, err := redis.Int64(c.Do("ZSCORE", p.key, name)); err == nil {
return score
}
return 0
}
func (p *RedisSortedSet) ZCount(min, max int64) int64 {
c := p.rp.Get()
defer c.Close()
count := func(key string) int64 {
if v, err := redis.Int64(c.Do("ZCOUNT", key, min, max)); err == nil {
return v
}
return 0
}
total := count(p.key)
return total
}
func (p *RedisSortedSet) ZRangeByScore(min, max int64) []string {
c := p.rp.Get()
defer c.Close()
if skeys, err := redis.Strings(c.Do("ZRANGEBYSCORE", p.key, min, max)); err == nil {
return skeys
}
return []string{}
}