From 97b96f6992a477f25b86b5af8340ae6218a38c67 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Tue, 26 Nov 2019 10:09:42 -0800 Subject: [PATCH] Use lua script to implement (*rdb).moveAll --- rdb.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rdb.go b/rdb.go index 67dd2d6..34493f1 100644 --- a/rdb.go +++ b/rdb.go @@ -135,15 +135,15 @@ func (r *rdb) listQueues() []string { // moveAll moves all tasks from src list to dst list. func (r *rdb) moveAll(src, dst string) error { - // TODO(hibiken): Lua script - txf := func(tx *redis.Tx) error { - length := tx.LLen(src).Val() - for i := 0; i < int(length); i++ { - tx.RPopLPush(src, dst) - } - return nil - } - return r.client.Watch(txf, src) + script := redis.NewScript(` + local len = redis.call("LLEN", KEYS[1]) + for i = len, 1, -1 do + redis.call("RPOPLPUSH", KEYS[1], KEYS[2]) + end + return len + `) + _, err := script.Run(r.client, []string{src, dst}).Result() + return err } // forward moves all tasks with a score less than the current unix time