From 488e89a720b45041a023d25d414b54fe6b6152c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Erik=20St=C3=B8wer?= Date: Mon, 12 Apr 2021 21:07:07 +0200 Subject: [PATCH] Call hget only when needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tom Erik Støwer --- .../main/java/org/redisson/jcache/JCache.java | 92 ++++++++++++------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/redisson/src/main/java/org/redisson/jcache/JCache.java b/redisson/src/main/java/org/redisson/jcache/JCache.java index 42cb4796c..df82fac09 100644 --- a/redisson/src/main/java/org/redisson/jcache/JCache.java +++ b/redisson/src/main/java/org/redisson/jcache/JCache.java @@ -392,24 +392,27 @@ public class JCache extends RedissonObject implements Cache, CacheAs if (containsKey(key)) { Long updateTimeout = getUpdateTimeout(); List res = evalWrite(getRawName(), codec, RedisCommands.EVAL_LIST, - "local value = redis.call('hget', KEYS[1], ARGV[4]);" - +"if ARGV[2] == '0' then " + "if ARGV[2] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[4]); " + "redis.call('zrem', KEYS[2], ARGV[4]); " + + "local value = redis.call('hget', KEYS[1], ARGV[4]);" + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[6]); " + "local syncs = redis.call('publish', KEYS[7], syncMsg); " + "return {0, syncs};" + "elseif ARGV[2] ~= '-1' then " - + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " - + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "local oldValueRequired = tonumber(redis.call('get', KEYS[9])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], -1, ARGV[6]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[4]);" + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], string.len(tostring(value)), tostring(value), ARGV[6]); " + "end; " @@ -417,13 +420,15 @@ public class JCache extends RedissonObject implements Cache, CacheAs + "local syncs = redis.call('publish', KEYS[8], syncMsg); " + "return {1, syncs};" + "else " - + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "local oldValueRequired = tonumber(redis.call('get', KEYS[9])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], -1, ARGV[6]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[4]);" + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], string.len(tostring(value)), tostring(value), ARGV[6]); " + "end; " @@ -500,23 +505,26 @@ public class JCache extends RedissonObject implements Cache, CacheAs "exists = false;" + "end;" + "if exists then " - + "local value = redis.call('hget', KEYS[1], ARGV[i]);" + "if ARGV[2] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[i]); " + "redis.call('zrem', KEYS[2], ARGV[i]); " + + "local value = redis.call('hget', KEYS[1], ARGV[i]);" + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[i]), ARGV[i], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[i]), ARGV[i], string.len(tostring(value)), tostring(value), ARGV[4]); " + "syncs = syncs + redis.call('publish', KEYS[7], syncMsg); " + "elseif ARGV[2] ~= '-1' then " - + "redis.call('hset', KEYS[1], ARGV[i], ARGV[i+1]); " - + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[i]); " + "local oldValueRequired = tonumber(redis.call('get', KEYS[9])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[i], ARGV[i+1]); " + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[i]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[i]), ARGV[i], string.len(ARGV[i+1]), ARGV[i+1], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[i]), ARGV[i], string.len(ARGV[i+1]), ARGV[i+1], -1, ARGV[4]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[i]);" + + "redis.call('hset', KEYS[1], ARGV[i], ARGV[i+1]); " + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[i]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[i]), ARGV[i], string.len(ARGV[i+1]), ARGV[i+1], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[i]), ARGV[i], string.len(ARGV[i+1]), ARGV[i+1], string.len(tostring(value)), tostring(value), ARGV[4]); " + "end; " @@ -524,13 +532,15 @@ public class JCache extends RedissonObject implements Cache, CacheAs + "syncs = syncs + redis.call('publish', KEYS[8], syncMsg); " + "added = added + 1;" + "else " - + "redis.call('hset', KEYS[1], ARGV[i], ARGV[i+1]); " + "local oldValueRequired = tonumber(redis.call('get', KEYS[9])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[i], ARGV[i+1]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[i]), ARGV[i], string.len(ARGV[i+1]), ARGV[i+1], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[i]), ARGV[i], string.len(ARGV[i+1]), ARGV[i+1], -1, ARGV[4]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[i]);" + + "redis.call('hset', KEYS[1], ARGV[i], ARGV[i+1]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[i]), ARGV[i], string.len(ARGV[i+1]), ARGV[i+1], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[i]), ARGV[i], string.len(ARGV[i+1]), ARGV[i+1], string.len(tostring(value)), tostring(value), ARGV[4]); " + "end; " @@ -627,24 +637,27 @@ public class JCache extends RedissonObject implements Cache, CacheAs "exists = false;" + "end;" + "if exists then " - + "local value = redis.call('hget', KEYS[1], ARGV[4]);" + "if ARGV[2] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[4]); " + "redis.call('zrem', KEYS[2], ARGV[4]); " + + "local value = redis.call('hget', KEYS[1], ARGV[4]);" + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[6]); " + "local syncs = redis.call('publish', KEYS[7], syncMsg); " + "return {0, syncs};" + "elseif ARGV[2] ~= '-1' then " - + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " - + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "local oldValueRequired = tonumber(redis.call('get', KEYS[9])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], -1, ARGV[6]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[4]);" + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], string.len(tostring(value)), tostring(value), ARGV[6]); " + "end; " @@ -652,13 +665,15 @@ public class JCache extends RedissonObject implements Cache, CacheAs + "local syncs = redis.call('publish', KEYS[8], syncMsg); " + "return {1, syncs};" + "else " - + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "local oldValueRequired = tonumber(redis.call('get', KEYS[9])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], -1, ARGV[6]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[4]);" + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], string.len(tostring(value)), tostring(value), ARGV[6]); " + "end; " @@ -2282,29 +2297,32 @@ public class JCache extends RedissonObject implements Cache, CacheAs Long updateTimeout = getUpdateTimeout(); double syncId = ThreadLocalRandom.current().nextDouble(); Long syncs = evalWrite(getRawName(), codec, RedisCommands.EVAL_LONG, - "local value = redis.call('hget', KEYS[1], ARGV[4]); " - + "if ARGV[2] == '0' then " + "if ARGV[2] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[4]); " + "redis.call('zrem', KEYS[2], ARGV[4]); " + + "local value = redis.call('hget', KEYS[1], ARGV[4]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[7]); " + "return redis.call('publish', KEYS[5], syncMsg); " - + "elseif ARGV[2] ~= '-1' then " - + "redis.call('hset', KEYS[1], ARGV[4], ARGV[6]); " - + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + + "elseif ARGV[2] ~= '-1' then " + "local oldValueRequired = tonumber(redis.call('get', KEYS[7])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[6]); " + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[4]), ARGV[4], string.len(ARGV[6]), ARGV[6], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[4]), ARGV[4], string.len(ARGV[6]), ARGV[6], -1, ARGV[7]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[4]); " + + "redis.call('hset', KEYS[1], ARGV[4], ARGV[6]); " + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[6]), ARGV[6], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[6]), ARGV[6], string.len(tostring(value)), tostring(value), ARGV[7]); " + "end; " + "redis.call('publish', KEYS[4], msg); " + "return redis.call('publish', KEYS[6], syncMsg); " - + "else " + + "else " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[6]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[6]), ARGV[6]); " + "redis.call('publish', KEYS[4], msg); " @@ -2497,36 +2515,41 @@ public class JCache extends RedissonObject implements Cache, CacheAs double syncId = ThreadLocalRandom.current().nextDouble(); Long updateTimeout = getUpdateTimeout(); Long syncs = evalWrite(getRawName(), codec, RedisCommands.EVAL_LONG, - "local value = redis.call('hget', KEYS[1], ARGV[3]); " - + "if ARGV[1] == '0' then " + "if ARGV[1] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[3]); " + "redis.call('zrem', KEYS[2], ARGV[3]); " + + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value), ARGV[5]); " + "return redis.call('publish', KEYS[5], syncMsg); " + "elseif ARGV[1] ~= '-1' then " - + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " - + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + "local oldValueRequired = tonumber(redis.call('get', KEYS[7])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], -1, ARGV[5]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[5]); " + "end; " + "redis.call('publish', KEYS[4], msg); " + "return redis.call('publish', KEYS[6], syncMsg); " - + "else " - + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + + "else " + "local oldValueRequired = tonumber(redis.call('get', KEYS[7])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], -1, ARGV[5]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[5]); " + "end; " @@ -2660,36 +2683,41 @@ public class JCache extends RedissonObject implements Cache, CacheAs Long updateTimeout = getUpdateTimeout(); double syncId = ThreadLocalRandom.current().nextDouble(); Long syncs = evalWrite(getRawName(), codec, RedisCommands.EVAL_LONG, - "local value = redis.call('hget', KEYS[1], ARGV[3]); " - + "if ARGV[1] == '0' then " + "if ARGV[1] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[3]); " + "redis.call('zrem', KEYS[2], ARGV[3]); " + + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value), ARGV[5]); " + "return redis.call('publish', KEYS[5], msg); " - + "elseif ARGV[1] ~= '-1' then " - + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " - + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + + "elseif ARGV[1] ~= '-1' then " + "local oldValueRequired = tonumber(redis.call('get', KEYS[7])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], -1, ARGV[5]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[5]); " + "end; " + "redis.call('publish', KEYS[4], msg); " + "return redis.call('publish', KEYS[6], syncMsg); " - + "else " - + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + + "else " + "local oldValueRequired = tonumber(redis.call('get', KEYS[7])); " + "local msg, syncMsg; " + "if oldValueRequired == nil or oldValueRequired < 1 then " + + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + "msg = struct.pack('Lc0Lc0h', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], -1); " + "syncMsg = struct.pack('Lc0Lc0hd', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], -1, ARGV[5]); " + "else " + + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + "msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "syncMsg = struct.pack('Lc0Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[5]); " + "end; " -- GitLab