php - Is it possible to check if a Redis set key exists and if it doesn't exist set it, if it does exist then exit, all as a transaction -


i want transaction because key serve "lock" later actions. if happens:

  1. check exists
  2. establish doesn't exist
  3. someone else checks exists whilst establishing doesn't exist
  4. set because doesn't exist , actions
  5. second person sets @ same time , actions when shouldn't allowed to.

well, if wanted know nature of redis transactions: http://redis.io/topics/transactions

in particular, part:

"all commands in transaction serialized , executed sequentially. can never happen request issued client served in middle of execution of redis transaction. guarantees commands executed single isolated operation."

if want single command adds if not exists, http://redis.io/commands/setnx link docs setnx that.

with transactions, start calling multi, send in whatever commands wish, call exec commence series of commands. guaranteed atomic, no other command can executed until sequence done.

hth


Comments