使用redis事物解決stringRedisTemplate.setIfAbsent()並設置過期時間遇到的問題


spring-date-redis版本:1.6.2
場景:在使用setIfAbsent(key,value)時,想對key設置一個過期時間,同時需要用到setIfAbsent的返回值來指定之后的流程,所以使用了以下代碼:

boolean store = stringRedisTemplate.opsForValue().setIfAbsent(key,value); if(store){ stringRedisTemplate.expire(key,timeout); // todo something... }

這段代碼是有問題的:當setIfAbsent成功之后斷開連接,下面設置過期時間的代碼stringRedisTemplate.expire(key,timeout); 是無法執行的,這時候就會有大量沒有過期時間的數據存在數據庫。想到一個辦法就是添加事務管理,修改后的代碼如下:

stringRedisTemplate.setEnableTransactionSupport(true); stringRedisTemplate.multi(); boolean store = stringRedisTemplate.opsForValue().setIfAbsent(key,value); if(store){ stringRedisTemplate.expire(key,timeout); } stringRedisTemplate.exec(); if(store){ // todo something... }

這樣就保證了整個流程的一致性。本因為這樣就可以了,可是事實總是不盡人意,因為我在文檔中發現了以下內容:
圖片描述

加了事務管理之后,setIfAbsent的返回值竟然是null,這樣就沒辦法再進行之后的判斷了。

好吧,繼續解決:

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...

上邊的代碼其實還是有問題的,當出現並發時,String result = stringRedisTemplate.opsForValue().get(key); 這里就會有多個線程同時拿到為空的key,然后同時寫入臟數據。


最終解決方法:

  1. 使用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會返回事務內每一個操作的結果,如果setIfAbsent操作失敗后,result[0]會為false。 if(true == result[0]){ // todo something... }
  1. 將redis版本升級到2.1以上,然后使用

圖片描述
直接在setIfAbsent中設置過期時間

update : 
java 使用redis的事務時不能直接用Api中的multi()和exec(),這樣multi()和exec()兩次使用的stringRedisTemplate不是一個connect,會導致死鎖,正確方式如下:

    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); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM