Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis: Atomic get and conditional set

I'd like to perform an atomic GET in Redis, and if the value returned is equal to some expected value, I'd like to do a SET, but I want to chain all of this together as one atomic operation. (I'm trying to set a flag that indicates whether any process is writing data to disk, as only one process may be permitted to do so.)

Is it possible to accomplish this with Redis?

I have seen documentation on MULTI operations but I haven't seen conditional operations i MULTI operations. Any suggestions others can offer with this would be greatly appreciated!

like image 777
duhaime Avatar asked May 12 '18 17:05

duhaime


1 Answers

You can do both the GET and set operations on the redis server itself using Lua scripts. They're atomic and allow you to add logic too.

like image 170
Chris Tanner Avatar answered Oct 19 '22 05:10

Chris Tanner