src/main/java/com/hx/redisson/config/RedissonUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/redisson/manager/RedisManager.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/redisson/register/RedisRegister.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
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 锁key * @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; } } 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; } /** * 根据数据库序号,返回对应的RedisTemplate @@ -38,4 +42,5 @@ return redisTemplate; } } 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); // 构造RedissonConnectionFactory 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("《《《动态注册bean结束》》》");