-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
50 lines (41 loc) · 1000 Bytes
/
pipeline.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
package topic
import (
"crypto/rand"
"encoding/binary"
"fmt"
"strconv"
"time"
redis "gopkg.in/redis.v5"
)
var (
randomString = func() string {
var n uint64
binary.Read(rand.Reader, binary.LittleEndian, &n)
rndStr := strconv.FormatUint(n, 36)
tsStr := strconv.FormatInt(time.Now().UnixNano(), 36)
return tsStr + rndStr
}
)
type pipelineImpl struct {
*storeImpl
tx *redis.Pipeline
}
func (p *pipelineImpl) Union(dest string, keys ...string) {
p.tx.SUnionStore(dest, keys...)
p.tx.Expire(dest, p.sessionTTL)
}
func (p *pipelineImpl) Inter(dest string, keys ...string) {
p.tx.SInterStore(dest, keys...)
p.tx.Expire(dest, p.sessionTTL)
}
func (p *pipelineImpl) Diff(dest string, keys ...string) {
p.tx.SDiffStore(dest, keys...)
p.tx.Expire(dest, p.sessionTTL)
}
func (p *pipelineImpl) Source(key string) string {
return fmt.Sprintf(p.topicKeyFormat, key)
}
func (p *pipelineImpl) Session() string {
key := randomString()
return fmt.Sprintf(p.sessionKeyFormat, key)
}