I want to atomically insert or update a key, insert this key into an index and, optionally, increment a count. To this end, I wrote the following Lua script. In this script, KEYS[1]
is the element key, KEYS[2]
is the index key, ARGV[1]
is the object stored at KEYS[1]
and ARGV[2]
is a score.
if not redis.call('EXISTS', KEYS[1]) then redis.call('INCR', KEYS[2] .. ":num");endredis.call('SET', KEYS[1], ARGV[1]);redis.call('ZADD', KEYS[2] .. ":idx", tonumber(ARGV[2]), KEYS[1]);
To access Redis, I use a RedisTemplate<String, Object>
instance which uses GenericFastJsonRedisSerializer
to serialize values. A small working example:
public class Main { public static void main(String[] args) { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName("localhost"); configuration.setPort(6379); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration); connectionFactory.afterPropertiesSet(); RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setDefaultSerializer(new GenericFastJsonRedisSerializer()); template.setDefaultSerializer(new GenericFastJsonRedisSerializer()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericFastJsonRedisSerializer()); template.setHashKeySerializer(new GenericFastJsonRedisSerializer()); template.setHashValueSerializer(new GenericFastJsonRedisSerializer()); template.setConnectionFactory(connectionFactory); template.afterPropertiesSet(); RedisScript<Object> script = RedisScript.of(new PathResource(Paths.get("my-script.lua"))); // <- above script template.execute(script, Arrays.asList("value-key", "index-key"), new Object(), 1.0); }}
However, when I run the Lua script, I get the following error:
@user_script: 14: Lua redis() command arguments must be strings or integers
I suppose, this is because the serializer also serializes the score, so Lua cannot read it as number anymore. Hence, how can I avoid that the serializer is applied to all arguments, and only translating my object to JSON?