Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

python - Atomic Redis Operations with JSON

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...