package com.qqflow.engine.common.service; import org.redisson.api.*; import org.springframework.stereotype.Component; import java.time.Duration; import java.util.Collection; import java.util.Map; import java.util.concurrent.TimeUnit; @Component public class RedissonService { private final RedissonClient redissonClient; public RedissonService(RedissonClient redissonClient) { this.redissonClient = redissonClient; } // ==================== 分布式锁 ==================== /** 获取锁(阻塞直到获取成功) */ public RLock getLock(String key) { return redissonClient.getLock(key); } /** 尝试获取锁,默认等待 10 秒,持有 30 秒 */ public boolean tryLock(String key) { try { return redissonClient.getLock(key).tryLock(10, 30, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } } /** 尝试获取锁 */ public boolean tryLock(String key, long waitTime, long leaseTime, TimeUnit unit) { try { return redissonClient.getLock(key).tryLock(waitTime, leaseTime, unit); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return false; } } /** 释放锁 */ public void unlock(String key) { RLock lock = redissonClient.getLock(key); if (lock.isHeldByCurrentThread()) { lock.unlock(); } } public void unlock(RLock lock) { if (lock != null && lock.isHeldByCurrentThread()) { lock.unlock(); } } /** 是否被锁 */ public boolean isLocked(String key) { return redissonClient.getLock(key).isLocked(); } // ==================== 通用 Key-Value ==================== public void set(String key, T value) { redissonClient.getBucket(key).set(value); } public void set(String key, T value, long ttl, TimeUnit unit) { redissonClient.getBucket(key).set(value, ttl, unit); } public void set(String key, T value, Duration ttl) { redissonClient.getBucket(key).set(value, ttl); } @SuppressWarnings("unchecked") public T get(String key) { RBucket bucket = redissonClient.getBucket(key); return bucket.get(); } public boolean delete(String key) { return redissonClient.getBucket(key).delete(); } public boolean exists(String key) { return redissonClient.getBucket(key).isExists(); } /** 设置过期时间 */ public boolean expire(String key, long ttl, TimeUnit unit) { return redissonClient.getBucket(key).expire(ttl, unit); } // ==================== 原子操作 ==================== public RAtomicLong getAtomicLong(String key) { return redissonClient.getAtomicLong(key); } public long increment(String key) { return redissonClient.getAtomicLong(key).incrementAndGet(); } public long decrement(String key) { return redissonClient.getAtomicLong(key).decrementAndGet(); } // ==================== Map 操作 ==================== public Map getMap(String key) { return redissonClient.getMap(key); } public void putMapValue(String key, K field, V value) { redissonClient.getMap(key).put(field, value); } public V getMapValue(String key, K field) { return redissonClient.getMap(key).get(field); } // ==================== 限流器 ==================== public RRateLimiter getRateLimiter(String key) { return redissonClient.getRateLimiter(key); } /** 设置限流速率并获取令牌 */ public boolean tryAcquireRateLimiter(String key, long rate, long rateInterval, RateIntervalUnit unit) { RRateLimiter limiter = redissonClient.getRateLimiter(key); limiter.trySetRate(RateType.OVERALL, rate, rateInterval, unit); return limiter.tryAcquire(); } // ==================== 队列操作 ==================== /** 获取阻塞队列 */ public RBlockingQueue getBlockingQueue(String key) { return redissonClient.getBlockingQueue(key); } /** 入队 */ public boolean offer(String key, T value) { return redissonClient.getQueue(key).offer(value); } /** 出队 */ public T poll(String key) { return redissonClient.getQueue(key).poll(); } // ==================== Set 操作 ==================== public boolean sAdd(String key, String value) { return redissonClient.getSet(key).add(value); } public boolean sContains(String key, String value) { return redissonClient.getSet(key).contains(value); } public Collection sMembers(String key) { return redissonClient.getSet(key).readAll(); } // ==================== 底层客户端 ==================== public RedissonClient getClient() { return redissonClient; } }