feat(redis): add ScriptRun API and migrate EvalCtx to ScriptRun for limit, lock and bloom (#3087)
This commit is contained in:
@@ -87,6 +87,8 @@ type (
|
||||
FloatCmd = red.FloatCmd
|
||||
// StringCmd is an alias of redis.StringCmd.
|
||||
StringCmd = red.StringCmd
|
||||
// Script is an alias of redis.Script.
|
||||
Script = red.Script
|
||||
)
|
||||
|
||||
// New returns a Redis with given options.
|
||||
@@ -145,6 +147,11 @@ func newRedis(addr string, opts ...Option) *Redis {
|
||||
return r
|
||||
}
|
||||
|
||||
// NewScript returns a new Script instance.
|
||||
func NewScript(script string) *Script {
|
||||
return red.NewScript(script)
|
||||
}
|
||||
|
||||
// BitCount is redis bitcount command implementation.
|
||||
func (s *Redis) BitCount(key string, start, end int64) (int64, error) {
|
||||
return s.BitCountCtx(context.Background(), key, start, end)
|
||||
@@ -1630,6 +1637,25 @@ func (s *Redis) ScriptLoadCtx(ctx context.Context, script string) (string, error
|
||||
return conn.ScriptLoad(ctx, script).Result()
|
||||
}
|
||||
|
||||
// ScriptRun is the implementation of *redis.Script run command.
|
||||
func (s *Redis) ScriptRun(script *Script, keys []string, args ...any) (any, error) {
|
||||
return s.ScriptRunCtx(context.Background(), script, keys, args...)
|
||||
}
|
||||
|
||||
// ScriptRunCtx is the implementation of *redis.Script run command.
|
||||
func (s *Redis) ScriptRunCtx(ctx context.Context, script *Script, keys []string, args ...any) (val any, err error) {
|
||||
err = s.brk.DoWithAcceptable(func() error {
|
||||
conn, err := getRedis(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
val, err = script.Run(ctx, conn, keys, args...).Result()
|
||||
return err
|
||||
}, acceptable)
|
||||
return
|
||||
}
|
||||
|
||||
// Set is the implementation of redis set command.
|
||||
func (s *Redis) Set(key, value string) error {
|
||||
return s.SetCtx(context.Background(), key, value)
|
||||
|
||||
@@ -240,6 +240,24 @@ func TestRedis_Eval(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRedis_ScriptRun(t *testing.T) {
|
||||
runOnRedis(t, func(client *Redis) {
|
||||
sc := NewScript(`redis.call("EXISTS", KEYS[1])`)
|
||||
sc2 := NewScript(`return redis.call("EXISTS", KEYS[1])`)
|
||||
_, err := New(client.Addr, badType()).ScriptRun(sc, []string{"notexist"})
|
||||
assert.NotNil(t, err)
|
||||
_, err = client.ScriptRun(sc, []string{"notexist"})
|
||||
assert.Equal(t, Nil, err)
|
||||
err = client.Set("key1", "value1")
|
||||
assert.Nil(t, err)
|
||||
_, err = client.ScriptRun(sc, []string{"key1"})
|
||||
assert.Equal(t, Nil, err)
|
||||
val, err := client.ScriptRun(sc2, []string{"key1"})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(1), val)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRedis_GeoHash(t *testing.T) {
|
||||
runOnRedis(t, func(client *Redis) {
|
||||
_, err := client.GeoHash("parent", "child1", "child2")
|
||||
|
||||
@@ -17,17 +17,20 @@ const (
|
||||
randomLen = 16
|
||||
tolerance = 500 // milliseconds
|
||||
millisPerSecond = 1000
|
||||
lockCommand = `if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||
)
|
||||
|
||||
var (
|
||||
lockScript = NewScript(`if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||
redis.call("SET", KEYS[1], ARGV[1], "PX", ARGV[2])
|
||||
return "OK"
|
||||
else
|
||||
return redis.call("SET", KEYS[1], ARGV[1], "NX", "PX", ARGV[2])
|
||||
end`
|
||||
delCommand = `if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||
end`)
|
||||
delScript = NewScript(`if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("DEL", KEYS[1])
|
||||
else
|
||||
return 0
|
||||
end`
|
||||
end`)
|
||||
)
|
||||
|
||||
// A RedisLock is a redis lock.
|
||||
@@ -59,7 +62,7 @@ func (rl *RedisLock) Acquire() (bool, error) {
|
||||
// AcquireCtx acquires the lock with the given ctx.
|
||||
func (rl *RedisLock) AcquireCtx(ctx context.Context) (bool, error) {
|
||||
seconds := atomic.LoadUint32(&rl.seconds)
|
||||
resp, err := rl.store.EvalCtx(ctx, lockCommand, []string{rl.key}, []string{
|
||||
resp, err := rl.store.ScriptRunCtx(ctx, lockScript, []string{rl.key}, []string{
|
||||
rl.id, strconv.Itoa(int(seconds)*millisPerSecond + tolerance),
|
||||
})
|
||||
if err == red.Nil {
|
||||
@@ -87,7 +90,7 @@ func (rl *RedisLock) Release() (bool, error) {
|
||||
|
||||
// ReleaseCtx releases the lock with the given ctx.
|
||||
func (rl *RedisLock) ReleaseCtx(ctx context.Context) (bool, error) {
|
||||
resp, err := rl.store.EvalCtx(ctx, delCommand, []string{rl.key}, []string{rl.id})
|
||||
resp, err := rl.store.ScriptRunCtx(ctx, delScript, []string{rl.key}, []string{rl.id})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user