forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.go
45 lines (37 loc) · 894 Bytes
/
script.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
package redis
import (
"crypto/sha1"
"encoding/hex"
"io"
"strings"
)
type Script struct {
src, hash string
}
func NewScript(src string) *Script {
h := sha1.New()
io.WriteString(h, src)
return &Script{
src: src,
hash: hex.EncodeToString(h.Sum(nil)),
}
}
func (s *Script) Load(c *Client) *StringReq {
return c.ScriptLoad(s.src)
}
func (s *Script) Exists(c *Client) *BoolSliceReq {
return c.ScriptExists(s.src)
}
func (s *Script) Eval(c *Client, keys []string, args []string) *IfaceReq {
return c.Eval(s.src, keys, args)
}
func (s *Script) EvalSha(c *Client, keys []string, args []string) *IfaceReq {
return c.EvalSha(s.hash, keys, args)
}
func (s *Script) Run(c *Client, keys []string, args []string) *IfaceReq {
r := s.EvalSha(c, keys, args)
if err := r.Err(); err != nil && strings.HasPrefix(err.Error(), "NOSCRIPT ") {
return s.Eval(c, keys, args)
}
return r
}