Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HSET if key exist

Tags:

redis

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 ?

like image 917
Vivek Goel Avatar asked Dec 23 '12 09:12

Vivek Goel


1 Answers

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.

like image 93
luin Avatar answered Sep 27 '22 20:09

luin