Skip to content

Commit 5307646

Browse files
author
Emmanuel T Odeke
authored
Merge pull request #8 from johanbrandhorst/add-script-do-context
redis: add DoContext to Script type
2 parents 2dce7e1 + 120614b commit 5307646

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

redis/script.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
package redis
1616

1717
import (
18+
"context"
1819
"crypto/sha1"
1920
"encoding/hex"
2021
"io"
2122
"strings"
23+
24+
"go.opencensus.io/trace"
2225
)
2326

2427
// Script encapsulates the source, hash and key count for a Lua script. See
@@ -64,10 +67,22 @@ func (s *Script) Hash() string {
6467
// script using the EVALSHA command. If the command fails because the script is
6568
// not loaded, then Do evaluates the script using the EVAL command (thus
6669
// causing the script to load).
67-
func (s *Script) Do(c Conn, keysAndArgs ...interface{}) (interface{}, error) {
68-
v, err := c.Do("EVALSHA", s.args(s.hash, keysAndArgs)...)
70+
func (s *Script) Do(c ConnWithContext, keysAndArgs ...interface{}) (interface{}, error) {
71+
return s.DoContext(context.Background(), c, keysAndArgs...)
72+
}
73+
74+
// DoContext evaluates the script. Under the covers, DoContext optimistically evaluates the
75+
// script using the EVALSHA command. If the command fails because the script is
76+
// not loaded, then DoContext evaluates the script using the EVAL command (thus
77+
// causing the script to load).
78+
func (s *Script) DoContext(ctx context.Context, c ConnWithContext, keysAndArgs ...interface{}) (interface{}, error) {
79+
ctx, span := trace.StartSpan(ctx, "redis.(*Script).DoContext")
80+
defer span.End()
81+
82+
v, err := c.DoContext(ctx, "EVALSHA", s.args(s.hash, keysAndArgs)...)
6983
if e, ok := err.(Error); ok && strings.HasPrefix(string(e), "NOSCRIPT ") {
70-
v, err = c.Do("EVAL", s.args(s.src, keysAndArgs)...)
84+
span.Annotate(nil, "missed script cache")
85+
v, err = c.DoContext(ctx, "EVAL", s.args(s.src, keysAndArgs)...)
7186
}
7287
return v, err
7388
}

0 commit comments

Comments
 (0)