-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathredis.go
44 lines (39 loc) · 1.38 KB
/
redis.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
package redis
import (
"context"
"crypto/sha1"
"encoding/hex"
"io"
"time"
)
// Pool maintains a pool of Redis connections.
type Pool interface {
Get(ctx context.Context) (Conn, error)
}
// Conn is a single Redis connection.
type Conn interface {
Get(name string) (string, error)
Set(name string, value string) (bool, error)
SetNX(name string, value string, expiry time.Duration) (bool, error)
Eval(script *Script, keysAndArgs ...interface{}) (interface{}, error)
PTTL(name string) (time.Duration, error)
Close() error
}
// Script encapsulates the source, hash and key count for a Lua script.
// Taken from https://github.com/gomodule/redigo/blob/46992b0f02f74066bcdfd9b03e33bc03abd10dc7/redis/script.go#L24-L30
type Script struct {
KeyCount int
Src string
Hash string
}
// NewScript returns a new script object. If keyCount is greater than or equal
// to zero, then the count is automatically inserted in the EVAL command
// argument list. If keyCount is less than zero, then the application supplies
// the count as the first value in the keysAndArgs argument to the Do, Send and
// SendHash methods.
// Taken from https://github.com/gomodule/redigo/blob/46992b0f02f74066bcdfd9b03e33bc03abd10dc7/redis/script.go#L32-L41
func NewScript(keyCount int, src string) *Script {
h := sha1.New()
_, _ = io.WriteString(h, src)
return &Script{keyCount, src, hex.EncodeToString(h.Sum(nil))}
}