2

How can one do this? Using Jedis and Java.

I am working with hset right now, but does no really matter.

Does one have to send along lua code to achieve this? How is that done with Jedis?

mjs
  • 21,431
  • 31
  • 118
  • 200

1 Answers1

1

Here is a Check-n-set for HSET command using LUA.

Jedis jedis;
String key, field, oldValueToCheck, newValueToSet;

jedis.eval("if redis.call('HGET', KEYS[1], ARGV[1]) == ARGV[3]"
    + " then return redis.call('HSET', KEYS[1], ARGV[1], ARGV[2])"
    + " else return 'NA' end", 1, key, field, newValueToSet, oldValueToCheck);
sazzad
  • 5,740
  • 6
  • 25
  • 42
  • thank you. Do you know how one can send null as a value for oldValueToSet ? null is not working. 'NA' ? from jedis side that is. – mjs Jun 27 '21 at 18:16
  • you can't. Redis doesn't allow null. Specially handling 'NA'/'NULL' string from Java can be an option. – sazzad Jun 27 '21 at 19:26
  • It is capable of returning null though. Lua hget call will return null as well if none exist. I don't see why there is not a representation of null in redis. I read something that there is a way but I could not get it to work. Uno momento. – mjs Jun 27 '21 at 19:31
  • https://stackoverflow.com/a/51485500/961018 https://redis.io/topics/protocol#nil-reply Seems this is only for replies? This does not make sense. I have to send in some special value for it which will only be valid in this one instance where my code use it. Should have known better than to rely on something relying on a language like LUA. I don't understand how they have no representation for an incoming null value. Zero byte array turns into god knows what. I am unable to log it from lua code. Is there a way? – mjs Jun 27 '21 at 19:35