-
Notifications
You must be signed in to change notification settings - Fork 4
/
seq.go
63 lines (55 loc) · 1007 Bytes
/
seq.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
package Figo
import (
"github.com/garyburd/redigo/redis"
"github.com/quexer/utee"
"sync"
)
type Seq interface {
Next() int64
}
type SeqRedis struct {
rp *redis.Pool
key string
}
func NewSeqRedis(rp *redis.Pool, key string, conf ...int64) *SeqRedis {
if len(conf) > 0 {
c := rp.Get()
defer c.Close()
start := conf[0] - 1
v, err := redis.Int64(c.Do("GET", key))
if err != redis.ErrNil {
utee.Chk(err)
}
existFlag := (err != redis.ErrNil && v > start)
if !existFlag {
c.Do("SET", key, conf[0]-1)
}
}
return &SeqRedis{
rp: rp,
key: key,
}
}
func (p *SeqRedis) Next() int64 {
c := p.rp.Get()
defer c.Close()
v, err := redis.Int64(c.Do("INCR", p.key))
utee.Chk(err)
return v
}
type SeqMem struct {
lock sync.Mutex
counter int64
}
func NewSeqMem(conf ...int64) *SeqMem {
if len(conf) > 0 {
return &SeqMem{counter: conf[0] - 1}
}
return &SeqMem{}
}
func (p *SeqMem) Next() int64 {
p.lock.Lock()
defer p.lock.Unlock()
p.counter++
return p.counter
}