-
Notifications
You must be signed in to change notification settings - Fork 71
/
utils.go
55 lines (48 loc) · 1.13 KB
/
utils.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
package rockscache
import (
"context"
"log"
"runtime/debug"
"time"
"github.com/redis/go-redis/v9"
)
var verbose = false
// SetVerbose sets verbose mode.
func SetVerbose(v bool) {
verbose = v
}
func debugf(format string, args ...interface{}) {
if verbose {
log.Printf(format, args...)
}
}
func now() int64 {
return time.Now().Unix()
}
func callLua(ctx context.Context, rdb redis.Scripter, script *redis.Script, keys []string, args []interface{}) (interface{}, error) {
debugf("callLua: script=%s, keys=%v, args=%v", script.Hash(), keys, args)
r := script.EvalSha(ctx, rdb, keys, args)
if redis.HasErrorPrefix(r.Err(), "NOSCRIPT") {
// try load script
if err := script.Load(ctx, rdb).Err(); err != nil {
debugf("callLua: load script failed: %v", err)
r = script.Eval(ctx, rdb, keys, args) // fallback to EVAL
} else {
r = script.EvalSha(ctx, rdb, keys, args) // retry EVALSHA
}
}
v, err := r.Result()
if err == redis.Nil {
err = nil
}
debugf("callLua result: v=%v, err=%v", v, err)
return v, err
}
func withRecover(f func()) {
defer func() {
if r := recover(); r != nil {
debug.PrintStack()
}
}()
f()
}