Redis?Lua同步鎖實現(xiàn)源碼解析
更新時間:2023年05月18日 10:43:23 作者:多喝灬丶燙水
這篇文章主要為大家介紹了Redis?Lua同步鎖實現(xiàn)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
Redis+Lua同步鎖
Jedis配置
@Configuration @Getter @Setter @Slf4j @ConfigurationProperties(prefix = "jedis") public class JedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; private int timeout; private int maxTotal; private int maxIdle; private int minIdle; @Bean public JedisPool jedisPool() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMinIdle(minIdle); jedisPoolConfig.setMaxTotal(maxTotal); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); log.info("JedisPool連接成功:" + host + "\t" + port); return jedisPool; } }
Jedis工具類→獲取jedis
@Component public class JedisUtil { @Resource private JedisPool jedisPool; /** * 獲取Jedis資源 */ public Jedis getJedis() { return jedisPool.getResource(); } /** * 釋放Jedis連接 */ public void close(Jedis jedis) { if (jedis != null) { jedis.close(); } } }
redis 鎖工具類
public class RedisLockUtil { private static final Long RELEASE_SUCCESS = 1L; private static final String PREFIX = "API_LOCK_"; /** * 釋放分布式鎖 * * @param jedis * @param lockKey * @param valve * @return boolean * @author ll * @date 2023/02/09 14:31 */ public static boolean unLock(Jedis jedis, String lockKey, String valve) { String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; Object result = jedis.eval(script, Collections.singletonList(PREFIX + lockKey), Collections.singletonList(PREFIX + valve)); if (RELEASE_SUCCESS.equals(result)) { return true; } return false; } /** * 加分布式鎖 * * @param jedis * @param lockKey * @param valve * @param timeout * @return boolean * @author ll * @date 2023/02/09 14:31 */ public static boolean lock(Jedis jedis, String lockKey, String valve, int timeout) { String script = "if redis.call('setnx',KEYS[1],ARGV[1]) == 1 then" + " redis.call('expire',KEYS[1],ARGV[2]) return 1 else return 0 end"; Object result = jedis.eval(script, Collections.singletonList(PREFIX + lockKey), Lists.newArrayList(PREFIX + valve, String.valueOf(timeout))); //判斷是否成功 if (RELEASE_SUCCESS.equals(result)) { return true; } return false; } }
加鎖示例(jedis+lua)
@Slf4j @Component public class InterfaceEventListener { @Resource private JedisUtil jedisUtil; @Value("${jedis.lock.cycle-number}") private int cycleNumber; @Value("${jedis.lock.expire-time}") private int expireTime; @Value("${jedis.lock.sleep-time}") private int sleepTime; @Value("${spring.redis.database}") private int database; public void onApplicationEvent(InterfaceEvent event) { Jedis jedis = jedisUtil.getJedis(); jedis.select(database); boolean unLock = false; boolean lock; int currentNumber = 0; try { do { lock = RedisLockUtil.lock(jedis, "lockKey", "valve", expireTime); if (lock) { try { //todo 加鎖的代碼 } catch (Exception e) { log.error(e.getMessage()); e.printStackTrace(); } finally { unLock = RedisLockUtil.unLock(jedis, detectCode, detectCode); } } currentNumber++; Thread.sleep(sleepTime); } while (!unLock && currentNumber < cycleNumber); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); } finally { jedisUtil.close(jedis); } } }
以上就是Redis Lua同步鎖實現(xiàn)源碼解析的詳細內(nèi)容,更多關(guān)于Redis Lua同步鎖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
聊聊使用RedisTemplat實現(xiàn)簡單的分布式鎖的問題
這篇文章主要介紹了使用RedisTemplat實現(xiàn)簡單的分布式鎖問題,文中給大家介紹在SpringBootTest中編寫測試模塊的詳細代碼,需要的朋友可以參考下2021-11-11關(guān)于SpringBoot 使用 Redis 分布式鎖解決并發(fā)問題
針對上面問題,一般的解決方案是使用分布式鎖來解決,本文通過場景分析給大家介紹關(guān)于SpringBoot 使用 Redis 分布式鎖解決并發(fā)問題,感興趣的朋友一起看看吧2021-11-11Redis Sorted Set 跳表的實現(xiàn)示例
本文詳細解析了Redis中SortedSet跳表的實現(xiàn)原理,闡述了跳表的基本概念、結(jié)構(gòu)及其在SortedSet中的應(yīng)用,同時也指出了跳表在實際使用中的優(yōu)勢和局限,可以更好地運用Redis的SortedSet,優(yōu)化高并發(fā)環(huán)境中的數(shù)據(jù)查詢與操作,感興趣的可以了解一下2024-10-10Redis的五種基本類型和業(yè)務(wù)場景和使用方式
Redis是一種高性能的鍵值存儲數(shù)據(jù)庫,支持多種數(shù)據(jù)結(jié)構(gòu)如字符串、列表、集合、哈希表和有序集合等,它提供豐富的API和持久化功能,適用于緩存、消息隊列、排行榜等多種場景,Redis能夠?qū)崿F(xiàn)高速讀寫操作,尤其適合需要快速響應(yīng)的應(yīng)用2024-10-10在Redis數(shù)據(jù)庫中實現(xiàn)分布式速率限制的方法
這篇文章主要介紹了在Redis數(shù)據(jù)庫中實現(xiàn)分布式速率限制的方法,文中展示了一個用Python編寫的應(yīng)用示例,需要的朋友可以參考下2015-06-06