基于redis setIfAbsent的使用說明
如果為空就set值,并返回1
如果存在(不為空)不進行操作,并返回0
很明顯,比get和set要好。因為先判斷get,再set的用法,有可能會重復(fù)set值。
setIfAbsent 和 setnx
setIfAbsent 是java中的方法
setnx 是 redis命令中的方法
setnx 例子
redis> SETNX mykey "Hello" (integer) 1 redis> SETNX mykey "World" (integer) 0 redis> GET mykey "Hello"
setIfAbsent 例子
代碼:
BoundValueOperations boundValueOperations = this.redisTemplate.boundValueOps(redisKey);
flag = boundValueOperations.setIfAbsent(value); // flag 表示的是否set
boundValueOperations.expire(seconds, TimeUnit.SECONDS);
if(!flag){ // 重復(fù)
repeatSerial.add(serialNo);
continue;
}else{// 沒有重復(fù)
norepeatSerial.add(serialNo);
}
補充:使用redis事物解決stringRedisTemplate.setIfAbsent()并設(shè)置過期時間遇到的問題
spring-date-redis版本:1.6.2
場景:
在使用setIfAbsent(key,value)時,想對key設(shè)置一個過期時間,同時需要用到setIfAbsent的返回值來指定之后的流程,所以使用了以下代碼:
boolean store = stringRedisTemplate.opsForValue().setIfAbsent(key,value);
if(store){
stringRedisTemplate.expire(key,timeout);
// todo something...
}
這段代碼是有問題的:當(dāng)setIfAbsent成功之后斷開連接,下面設(shè)置過期時間的代碼stringRedisTemplate.expire(key,timeout); 是無法執(zhí)行的,這時候就會有大量沒有過期時間的數(shù)據(jù)存在數(shù)據(jù)庫。想到一個辦法就是添加事務(wù)管理,修改后的代碼如下:
stringRedisTemplate.setEnableTransactionSupport(true);
stringRedisTemplate.multi();
boolean store = stringRedisTemplate.opsForValue().setIfAbsent(key,value);
if(store){
stringRedisTemplate.expire(key,timeout);
}
stringRedisTemplate.exec();
if(store){
// todo something...
}
這樣就保證了整個流程的一致性。本因為這樣就可以了,可是事實總是不盡人意,因為我在文檔中發(fā)現(xiàn)了以下內(nèi)容:

加了事務(wù)管理之后,setIfAbsent的返回值竟然是null,這樣就沒辦法再進行之后的判斷了。
好吧,繼續(xù)解決:
stringRedisTemplate.setEnableTransactionSupport(true);
stringRedisTemplate.multi();
String result = stringRedisTemplate.opsForValue().get(key);
if(StringUtils.isNotBlank(result)){
return false;
}
// 鎖的過期時間為1小時
stringRedisTemplate.opsForValue().set(key, value,timeout);
stringRedisTemplate.exec();
// todo something...
上邊的代碼其實還是有問題的,當(dāng)出現(xiàn)并發(fā)時,String result = stringRedisTemplate.opsForValue().get(key); 這里就會有多個線程同時拿到為空的key,然后同時寫入臟數(shù)據(jù)。
最終解決方法:
使用stringRedisTemplate.exec();的返回值判斷setIfAbsent是否成功
stringRedisTemplate.setEnableTransactionSupport(true);
stringRedisTemplate.multi();
stringRedisTemplate.opsForValue().setIfAbsent(lockKey,JSON.toJSONString(event));
stringRedisTemplate.expire(lockKey,Constants.REDIS_KEY_EXPIRE_SECOND_1_HOUR, TimeUnit.SECONDS);
List result = stringRedisTemplate.exec(); // 這里result會返回事務(wù)內(nèi)每一個操作的結(jié)果,如果setIfAbsent操作失敗后,result[0]會為false。
if(true == result[0]){
// todo something...
}
將redis版本升級到2.1以上,然后使用

直接在setIfAbsent中設(shè)置過期時間
update :
java 使用redis的事務(wù)時不能直接用Api中的multi()和exec(),這樣multi()和exec()兩次使用的stringRedisTemplate不是一個connect,會導(dǎo)致死鎖,正確方式如下:
private Boolean setLock(RecordEventModel event) {
String lockKey = event.getModel() + ":" + event.getAction() + ":" + event.getId() + ":" + event.getMessage_id();
log.info("lockKey : {}" , lockKey);
SessionCallback<Boolean> sessionCallback = new SessionCallback<Boolean>() {
List<Object> exec = null;
@Override
@SuppressWarnings("unchecked")
public Boolean execute(RedisOperations operations) throws DataAccessException {
operations.multi();
stringRedisTemplate.opsForValue().setIfAbsent(lockKey,JSON.toJSONString(event));
stringRedisTemplate.expire(lockKey,Constants.REDIS_KEY_EXPIRE_SECOND_1_HOUR, TimeUnit.SECONDS);
exec = operations.exec();
if(exec.size() > 0) {
return (Boolean) exec.get(0);
}
return false;
}
};
return stringRedisTemplate.execute(sessionCallback);
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
必須掌握的十個Lambda表達式簡化代碼提高生產(chǎn)力
這篇文章主要為大家介紹了必須掌握的十個Lambda表達式來簡化代碼提高生產(chǎn)力,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
java httpclient設(shè)置超時時間和代理的方法
這篇文章主要介紹了java httpclient設(shè)置超時時間和代理的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

