From 848054f126942c4a207b6256716a8642c8deea51 Mon Sep 17 00:00:00 2001 From: fwq <582742538@qq.com> Date: 星期二, 08 八月 2023 15:23:32 +0800 Subject: [PATCH] redisson原子加减操作 --- src/main/java/com/hx/redisson/manager/RedisManager.java | 5 ++ src/main/java/com/hx/redisson/register/RedisRegister.java | 4 ++ src/main/java/com/hx/redisson/config/RedissonUtil.java | 81 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/hx/redisson/config/RedissonUtil.java b/src/main/java/com/hx/redisson/config/RedissonUtil.java index 7957e3c..2d2fbce 100644 --- a/src/main/java/com/hx/redisson/config/RedissonUtil.java +++ b/src/main/java/com/hx/redisson/config/RedissonUtil.java @@ -1,12 +1,13 @@ package com.hx.redisson.config; import com.hx.redisson.manager.RedisManager; +import org.redisson.api.RLock; +import org.redisson.api.RedissonClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ZSetOperations; -import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.List; @@ -1607,4 +1608,82 @@ return null; } } + + /** + * 鍘熷瓙鍔� + * @param key 閿� + * @param num 鏁伴噺 + */ + public Long increment(String key,long num) { + try { + return redisTemplate.opsForValue().increment(key, num); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 鍘熷瓙鍔� + * @param key 閿� + * @param num 鏁伴噺 + * @param db 鏁版嵁搴� + */ + public Long increment(String key,long num,int db) { + try { + RedisTemplate redisTemplate = redisManager.getRedisTemplate(db); + return redisTemplate.opsForValue().increment(key, num); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 鍘熷瓙鍑� + * @param key 閿� + * @param num 鏁伴噺 + */ + public Long decrement(String key,long num) { + try { + return redisTemplate.opsForValue().decrement(key, num); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 鍘熷瓙鍑� + * @param key 閿� + * @param num 鏁伴噺 + * @param db 鏁版嵁搴� + */ + public Long decrement(String key,long num,int db) { + try { + RedisTemplate redisTemplate = redisManager.getRedisTemplate(db); + return redisTemplate.opsForValue().decrement(key, num); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + + /** + * 鑾峰彇绾㈤攣 + * @param key 閿乲ey + * @param db 鏁版嵁搴撶紪鍙� + */ + public RLock getRLock(String key,int db) { + RLock lock = null; + try { + RedissonClient redissonClient = RedisManager.redissonTemplateMap.get(db+""); + lock = redissonClient.getLock(key); + } catch (Exception e) { + e.printStackTrace(); + } + return lock; + } + } diff --git a/src/main/java/com/hx/redisson/manager/RedisManager.java b/src/main/java/com/hx/redisson/manager/RedisManager.java index e4f42c7..6798614 100644 --- a/src/main/java/com/hx/redisson/manager/RedisManager.java +++ b/src/main/java/com/hx/redisson/manager/RedisManager.java @@ -1,5 +1,6 @@ package com.hx.redisson.manager; +import org.redisson.api.RedissonClient; import org.springframework.data.redis.core.RedisTemplate; import java.util.Map; @@ -15,6 +16,8 @@ private Map<String, RedisTemplate> redisTemplateMap = new ConcurrentHashMap<>(); + public static Map<String, RedissonClient> redissonTemplateMap = new ConcurrentHashMap<>(); + /** * 鏋勯�犳柟娉曞垵濮嬪寲 redisTemplateMap 鐨勬暟鎹� * @@ -23,6 +26,7 @@ public RedisManager(Map<String, RedisTemplate> redisTemplateMap) { this.redisTemplateMap = redisTemplateMap; } + /** * 鏍规嵁鏁版嵁搴撳簭鍙凤紝杩斿洖瀵瑰簲鐨凴edisTemplate @@ -38,4 +42,5 @@ return redisTemplate; } + } diff --git a/src/main/java/com/hx/redisson/register/RedisRegister.java b/src/main/java/com/hx/redisson/register/RedisRegister.java index faf65e8..2be389c 100644 --- a/src/main/java/com/hx/redisson/register/RedisRegister.java +++ b/src/main/java/com/hx/redisson/register/RedisRegister.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import com.hx.redisson.entity.RedissondbConfigEntity; +import com.hx.redisson.manager.RedisManager; import org.apache.commons.collections.CollectionUtils; import org.redisson.Redisson; import org.redisson.api.RedissonClient; @@ -93,6 +94,7 @@ System.out.println("getHost:"+singleConfig.getAddress()); System.out.println("getHost:"+singleConfig.getPassword()); RedissonClient redissonClient = Redisson.create(config); + // 鏋勯�燫edissonConnectionFactory RedissonConnectionFactory redisConnectionFactory = new RedissonConnectionFactory(redissonClient); // bean瀹氫箟 @@ -105,6 +107,8 @@ redisTemplate(redisTemplate, redisConnectionFactory); // 娉ㄥ唽Bean registry.registerBeanDefinition("redisTemplate" + db, redisTemplate); + + RedisManager.redissonTemplateMap.put(db+"",redissonClient); }); } logger.info("銆娿�娿�婂姩鎬佹敞鍐宐ean缁撴潫銆嬨�嬨��"); -- Gitblit v1.8.0