From 61c6d7dd930ca60ac7a92b92b14742972a04e162 Mon Sep 17 00:00:00 2001 From: Julian Grinblat Date: Mon, 13 May 2024 23:54:56 +0900 Subject: [PATCH] fix: case where there are zero matched keys (#191) --- src/pdel.lua | 8 ++++++-- test/integration/index.js | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pdel.lua b/src/pdel.lua index a159a14..db36c45 100644 --- a/src/pdel.lua +++ b/src/pdel.lua @@ -8,12 +8,16 @@ if type(redis.replicate_commands) == "function" and redis.replicate_commands() t repeat cursor, keys = unpack(redis.call("SCAN", cursor, "MATCH", KEYS[1])) count = count + #keys - redis.call("DEL", unpack(keys)) + if #keys > 0 then + redis.call("DEL", unpack(keys)) + end until cursor == "0" return count else local keys = redis.call("KEYS", KEYS[1]) - redis.call("DEL", unpack(keys)) + if #keys > 0 then + redis.call("DEL", unpack(keys)) + end return #keys end diff --git a/test/integration/index.js b/test/integration/index.js index abd82b2..6471898 100644 --- a/test/integration/index.js +++ b/test/integration/index.js @@ -44,4 +44,6 @@ describe('integration', () => { 'bar:2', ])).to.become([null, null, 'val', 'val']); // eslint-disable-line unicorn/no-null }); + + it('should delete zero keys', () => expect(redis.pdel('foo:*')).to.become(0)); });