Просмотр исходного кода

暴露redis EvalSha 以及ScriptLoad接口 (#538)

Co-authored-by: shanehe <shanehe@zego.im>
hexiaoen 3 лет назад
Родитель
Сommit
b321622c95
2 измененных файлов с 29 добавлено и 3 удалено
  1. 16 1
      core/stores/redis/redis.go
  2. 13 2
      core/stores/redis/redis_test.go

+ 16 - 1
core/stores/redis/redis.go

@@ -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 {
@@ -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

+ 13 - 2
core/stores/redis/redis_test.go

@@ -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{
 		{