Vivek Goel
Vivek Goel

Reputation: 24140

HSET if key exist

Redis don't support HSET only if key exist. http://redis.io/commands#hash What will be the best way to achieve that functionality in client ?

Upvotes: 6

Views: 6373

Answers (1)

luin
luin

Reputation: 1995

It's easy to implement it on the client side using transaction.

WATCH hkey
isKeyExists = EXISTS hkey
if isKeyExists
  MULTI
  HSET hkey field value
  EXEC
else
  UNWATCH

When the hkey is removed after WATCH, the transaction will fail.

You can also use the scripting that was introduced in the Redis 2.6.

Upvotes: 10

Related Questions