package com.tools.common.redis;@b@@b@import org.redisson.api.RLock;@b@import org.redisson.api.RMapCache;@b@import org.redisson.api.RedissonClient;@b@@b@import java.util.concurrent.TimeUnit;@b@@b@/**@b@ * redis分布式锁@b@ * 爪哇笔记 https://blog.52itstyle.vip@b@ * @author 小柒2012@b@ */@b@public class RedissLockUtil {@b@@b@ private static RedissonClient redissonClient;@b@ @b@ public void setRedissonClient(RedissonClient locker) {@b@ redissonClient = locker;@b@ }@b@ @b@ /**@b@ * 加锁@b@ * @param lockKey@b@ * @return@b@ */@b@ public static RLock lock(String lockKey) {@b@ RLock lock = redissonClient.getLock(lockKey);@b@ lock.lock();@b@ return lock;@b@ }@b@@b@ /**@b@ * 释放锁@b@ * @param lockKey@b@ */@b@ public static void unlock(String lockKey) {@b@ RLock lock = redissonClient.getLock(lockKey);@b@ lock.unlock();@b@ }@b@ @b@ /**@b@ * 释放锁@b@ * @param lock@b@ */@b@ public static void unlock(RLock lock) {@b@ lock.unlock();@b@ }@b@@b@ /**@b@ * 带超时的锁@b@ * @param lockKey@b@ * @param timeout 超时时间 单位:秒@b@ */@b@ public static RLock lock(String lockKey, int timeout) {@b@ RLock lock = redissonClient.getLock(lockKey);@b@ lock.lock(timeout, TimeUnit.SECONDS);@b@ return lock;@b@ }@b@ @b@ /**@b@ * 带超时的锁@b@ * @param lockKey@b@ * @param unit 时间单位@b@ * @param timeout 超时时间@b@ */@b@ public static RLock lock(String lockKey, TimeUnit unit ,int timeout) {@b@ RLock lock = redissonClient.getLock(lockKey);@b@ lock.lock(timeout, unit);@b@ return lock;@b@ }@b@ @b@ /**@b@ * 尝试获取锁@b@ * @param lockKey@b@ * @param waitTime 最多等待时间@b@ * @param leaseTime 上锁后自动释放锁时间@b@ * @return@b@ */@b@ public static boolean tryLock(String lockKey, int waitTime, int leaseTime) {@b@ RLock lock = redissonClient.getLock(lockKey);@b@ try {@b@ return lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS);@b@ } catch (InterruptedException e) {@b@ return false;@b@ }@b@ }@b@ @b@ /**@b@ * 尝试获取锁@b@ * @param lockKey@b@ * @param unit 时间单位@b@ * @param waitTime 最多等待时间@b@ * @param leaseTime 上锁后自动释放锁时间@b@ * @return@b@ */@b@ public static boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {@b@ RLock lock = redissonClient.getLock(lockKey);@b@ try {@b@ return lock.tryLock(waitTime, leaseTime, unit);@b@ } catch (InterruptedException e) {@b@ return false;@b@ }@b@ }@b@@b@ /**@b@ * 初始红包数量@b@ * @param key@b@ * @param count@b@ */@b@ public void initCount(String key,int count) {@b@ RMapCache<String, Integer> mapCache = redissonClient.getMapCache("skill");@b@ mapCache.putIfAbsent(key,count,3,TimeUnit.DAYS);@b@ }@b@ /**@b@ * 递增@b@ * @param key@b@ * @param delta 要增加几(大于0)@b@ * @return@b@ */@b@ public int incr(String key, int delta) {@b@ RMapCache<String, Integer> mapCache = redissonClient.getMapCache("skill");@b@ if (delta < 0) {@b@ throw new RuntimeException("递增因子必须大于0");@b@ }@b@ return mapCache.addAndGet(key, 1);//加1并获取计算后的值@b@ }@b@@b@ /**@b@ * 递减@b@ * @param key 键@b@ * @param delta 要减少几(小于0)@b@ * @return@b@ */@b@ public int decr(String key, int delta) {@b@ RMapCache<String, Integer> mapCache = redissonClient.getMapCache("skill");@b@ if (delta < 0) {@b@ throw new RuntimeException("递减因子必须大于0");@b@ }@b@ return mapCache.addAndGet(key, -delta);//加1并获取计算后的值@b@ }@b@ public static RedissonClient getRedissonClient() {@b@ return redissonClient;@b@ }@b@}