網站首頁 編程語言 正文
RedisTemplate由于沒有setnx指令,所以需要自定義腳本時間
?一、請直接復制
package com.zy.base.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* redis 工具類
* @Author ZhangYi
*/
@Component
@Slf4j
public class RedisUtil {
@Resource
private RedisTemplate redisTemplate;
/**
* lua 腳本
*/
public static final String SETNX_SCRIPT = "return redis.call('setnx',KEYS[1], ARGV[1])";
/**
* redis實現分布式鎖
* @param key
* @return
*/
public boolean setNx(String key,int time) {
//自定義腳本
DefaultRedisScript script = new DefaultRedisScript<>(SETNX_SCRIPT, List.class);
//執行腳本,傳入參數,由于value沒啥用,這里隨便寫死的"1"
List rst = redisTemplate.execute(script, Collections.singletonList(key), "1");
//返回1,表示設置成功,拿到鎖
if(rst.get(0) == 1){
log.info(key+"成功拿到鎖");
//設置過期時間
expire(key,time);
log.info(key+"已成功設置過期時間:"+time +" 秒");
return true;
}else{
long expire = getExpire(key);
log.info(key+"未拿到到鎖,還有"+expire+"釋放");
return false;
}
}
/**
* 指定緩存失效時間
*
* @param key 鍵
* @param time 時間(秒)
* @return
*/
public void expire(String key, long time) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
/**
* 根據key 獲取過期時間
*
* @param key 鍵 不能為null
* @return 時間(秒) 返回0代表為永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 刪除緩存
*
* @param key 可以傳一個值 或多個
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete((Collection) CollectionUtils.arrayToList(key));
}
}
}
}
二、使用栗子
redisUtil.setNx("zyLock1",5);
過期時間是為了防止死鎖,當業務執行完畢,刪除key釋放鎖
原文鏈接:https://zhangyi520.blog.csdn.net/article/details/124926230
相關推薦
- 2022-07-23 Python實現環形鏈表_python
- 2023-10-27 解決webpack打包后圖片加載失敗的bug(適用于所有本地靜態資源)
- 2022-07-10 docker的安裝及常用命令
- 2022-07-13 Docker的安裝部署與優化
- 2022-11-18 C語言手寫多級時間輪定時器_C 語言
- 2022-07-12 從GitHub(git)上指定分支clone代碼
- 2022-05-15 C++11:lambda表達式詳細介紹
- 2022-11-13 Python?fileinput模塊應用詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支