-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.go
40 lines (35 loc) · 869 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
package golimit
import (
"crypto/sha1"
"encoding/hex"
"io"
)
// Redis interface
type Redis interface {
Eval(script string, keys []string, args ...interface{}) (interface{}, error)
EvalSha(sha1 string, keys []string, args ...interface{}) (interface{}, error, bool)
}
// Script struct
type Script struct {
redis Redis
src string
hash string
}
// NewScript returns new script config
func NewScript(redis Redis, src string) *Script {
h := sha1.New()
io.WriteString(h, src)
return &Script{
redis: redis,
src: src,
hash: hex.EncodeToString(h.Sum(nil)),
}
}
// Run script execution by using the EvalSha command
func (s *Script) Run(keys []string, args ...interface{}) (interface{}, error) {
result, err, noScript := s.redis.EvalSha(s.hash, keys, args...)
if noScript {
result, err = s.redis.Eval(s.src, keys, args...)
}
return result, err
}