redis setIfAbsent和 setnx 的区别与使用

如果为空就set值,并返回1
如果存在(不为空)不进行操作,并返回0

setIfAbsent 和 setnx 使用范围

setIfAbsent 是java中的方法
setnx 是 redis命令中的方法

setnx 例子

redis> SETNX testkey "test"
(integer) 1
redis> SETNX testkey "test"
(integer) 0
redis> GET testkey
"test"

setIfAbsent 例子

BoundValueOperations boundValueOperations = this.redisTemplate.boundValueOps(redisKey);
flag = boundValueOperations.setIfAbsent(value); // flag 表示的是否set
boundValueOperations.expire(seconds, TimeUnit.SECONDS);

if(!flag){ // 重复
    repeatSerial.add(serialNo);
    continue;
}else{// 没有重复
    norepeatSerial.add(serialNo);
}

————————————————
版权声明:本文为CSDN博主「chushiyunen」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/enthan809882/article/details/107783440

你可能感兴趣的:(记录使用,java)