Skip to content

Commit

Permalink
暴露redis EvalSha 以及ScriptLoad接口 (zeromicro#538)
Browse files Browse the repository at this point in the history
Co-authored-by: shanehe <[email protected]>
  • Loading branch information
hexiaoen and shanehe authored Mar 3, 2021
1 parent a25cba5 commit b321622
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
17 changes: 16 additions & 1 deletion core/stores/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,21 @@ func (s *Redis) Eval(script string, keys []string, args ...interface{}) (val int
return
}

// Eval is the implementation of redis eval command.
func (s *Redis) EvalSha(script string, keys []string, args ...interface{}) (val interface{}, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}

val, err = conn.EvalSha(script, keys, args...).Result()
return err
}, acceptable)

return
}

// Exists is the implementation of redis exists command.
func (s *Redis) Exists(key string) (val bool, err error) {
err = s.brk.DoWithAcceptable(func() error {
Expand Down Expand Up @@ -1672,7 +1687,7 @@ func (s *Redis) String() string {
return s.Addr
}

func (s *Redis) scriptLoad(script string) (string, error) {
func (s *Redis) ScriptLoad(script string) (string, error) {
conn, err := getRedis(s)
if err != nil {
return "", err
Expand Down
15 changes: 13 additions & 2 deletions core/stores/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,13 +947,24 @@ func TestRedisString(t *testing.T) {
func TestRedisScriptLoad(t *testing.T) {
runOnRedis(t, func(client *Redis) {
client.Ping()
_, err := NewRedis(client.Addr, "").scriptLoad("foo")
_, err := NewRedis(client.Addr, "").ScriptLoad("foo")
assert.NotNil(t, err)
_, err = client.scriptLoad("foo")
_, err = client.ScriptLoad("foo")
assert.NotNil(t, err)
})
}

func TestRedisEvalSha(t *testing.T) {
runOnRedis(t, func(client *Redis) {
client.Ping()
scriptHash, err := client.ScriptLoad(`return redis.call("EXISTS", KEYS[1])`)
assert.Nil(t, err)
result, err := client.EvalSha(scriptHash, []string{"key1"})
assert.Nil(t, err)
assert.Equal(t, int64(0), result)
})
}

func TestRedisToPairs(t *testing.T) {
pairs := toPairs([]red.Z{
{
Expand Down

0 comments on commit b321622

Please sign in to comment.