5
votes

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 ?

1

1 Answers

10
votes

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.