ZBLOG

redis setIfPresent

Redis does not have a specific command called setIfPresent. However, you can achieve similar functionality using the SETNX command.

The SETNX command sets the value of a key only if it does not already exist. If the key exists, then the command does nothing.

Here’s an example:

SETNX mykey "hello"

This command will set the value of mykey to "hello" only if mykey does not already exist. If mykey already has a value, then this command will do nothing.

You can use this command as a way to implement a “setIfPresent” behavior in Redis. For example:

SETNX mykey "newvalue"
GET mykey

In this example, if mykey already has a value, then the SETNX command will not set it to "newvalue". Instead, it will keep its existing value. The subsequent GET command will return either the original value or the new one depending on whether or not mykey existed before running SETNX.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=6328

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?