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/config/RedissonUtil.java | 81 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 80 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; + } + } -- Gitblit v1.8.0