luca3m / redis3m

A C++ Redis client
Apache License 2.0
190 stars 78 forks source link

How to implement lock using redis3m. #18

Closed nidhijha closed 9 years ago

nidhijha commented 9 years ago

Lets say my use case is, There are 2 processes trying to acquire lock on some key. One of them succeed, while other will just wait. Once first one is done updating the value it should release lock and then 2nd process can acquire lock if want or use the value updated by 1st one. I am talking about similar methods from StackExchange.Redis for c#

  1. LockTake
  2. LockRelease
  3. LockExtend
  4. LockQuery
luca3m commented 9 years ago

Lock are implemented in Redis using SET command and its options. StackExchange.Redis does the same as I can see on their code. So you can issue these commands using redis3m too, for example the lockTake should look in this way:

using namespace redis3m;
connection::ptr_t conn = connection::create();
conn->run(command("SET") << "lockname" << "EX" << expiretime << "NX");
// if response is OK you have got the lock, otherwise you don't have it

I called these common features patterns. You can find some here, they are included on redis3m. If you want you can create a new one for locks and we can include it on redis3m.

nidhijha commented 9 years ago

Thanks, I have implemented lock using SET, MULTI, EXEC ,WATCH commands.

luca3m commented 9 years ago

Cool! :)

On 06 Mar 2015, at 08:15, nidhijha notifications@github.com wrote:

Thanks, I have implemented lock using SET, MULTI, EXEC ,WATCH commands.

— Reply to this email directly or view it on GitHub.