| | |
| | | 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; |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |