Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to store a fairly complex JSON object in Redis but I can't see how I can make the transaction atomic, since I will have to get, edit, then set. In the time it's taken to edit, another get could have been called elsewhere which will eventually overwrite my data.

I have looked at Pipelines but it doesn't seem they will be useful, because the commands execute at the same time? Not possible to parse/edit before the set?

I also can't find a way to lock the document/hash during this proccess.

question from:https://stackoverflow.com/questions/66055250/atomic-redis-operations-with-json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
67 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have three options here:

  1. Use Redis Transactions:
WATCH mykey
GET mykey
// change the JSON
MULTI
SET mykey
EXEC
  1. Use Lua Script:

  2. Use the Redis Module RedisJSON which supports atomic commands like:

JSON.ARRINSERT & JSON.NUMINCRBY


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...