HSET 如果键存在 [英] HSET if key exist

查看:49
本文介绍了HSET 如果键存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Redis 仅在 key 存在时才支持 HSET.http://redis.io/commands#hash在客户端中实现该功能的最佳方式是什么?

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 ?

推荐答案

在客户端使用事务很容易实现.

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

WATCH后移除hkey,交易失败.

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

您也可以使用 Redis 2.6 中引入的脚本.

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

这篇关于HSET 如果键存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆