How to manipulate number in Redis using Lua Script -
i trying multiply 2 numbers stored in redis using lua script. getting classcastexception. point out wrong in program
jedis.set("one", "1"); jedis.set("two", "2"); string script = "return {tonumber(redis.call('get',keys[1])) * tonumber(redis.call('get',keys[2]))}"; string [] keys = new string[]{"one","two"}; object response = jedis.eval(script, 2, keys ); system.out.println(response);
throws
exception in thread "main" java.lang.classcastexception: java.lang.long cannot cast [b @ redis.clients.jedis.jedis.getevalresult(jedis.java:2806) @ redis.clients.jedis.jedis.eval(jedis.java:2766) @ com.test.jedis.script.simplescript.main(simplescript.java:18)
you can't cast table number in lua. want grab number of elements in table instead. can using last element point #
. also, i'd highly recommend separating out lua script rest of code, it's cleaner. lua script should like:
local first_key = redis.call('get',keys[1]) local second_key = redis.call('get',keys[2]) return #first_key * #second_key
edit: misunderstood question. op correctly pointed out trying multiple 2 numbers stored strings rather table length. in case:
local first_key = redis.call('get',keys[1]) if not tonumber(first_key) return "bad type on key[1]" end local second_key = redis.call('get',keys[2]) if not tonumber(second_key) return "bad type on key[2]" end return tonumber(first_key) * tonumber(second_key)
Comments
Post a Comment