0

I'm using jedis, and wish to get the result of a key and modify it and then store it back,

String dataToModify = jedis.get(parent_id);
//some modify  eg:  
modifiedData = dataToModify + "modify";
jedis.set(parent_id, modifiedData);

but I then realized that if someone else modified data in key parent_id, between this get and set, there will be collision. the watch-multi-exec does not work since I need to get the value of key parent_id during the transaction. Is there any other way to do this atomically? thanks

user2810081
  • 589
  • 1
  • 8
  • 27

2 Answers2

0

Not sure regarding the exact jedis syntax, but Redis' WATCH/MULTI/EXEC will let you do that (I.e. run the get/set flow atomically). Alternatively, if you implement your workflow in a server-side Lua script, that would also guarantee atomicity.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
0

How about saving the date in epoch (also known as unix time) value, this way you can treat your date as a number and use INCRBY command e.g.:
jedis.incrby(parent_id, modify);

Ofir Luzon
  • 10,635
  • 3
  • 41
  • 52