All checks were successful
Publish to Confluence / confluence (push) Successful in 1m3s
6.6 KiB
6.6 KiB
engine-starter-redis 使用教程
基于 Redis 的各种工具封装
具体使用可参考 engine-sample > engine-sample-starter-redis
功能特性
@YmIdempotent
: 基于 Redis 的幂等工具@YmRateLimit
: 基于 Redisson 的限流工具@Lock4j
: 基于 Lock4j 的分布式锁工具- 旧包中的
@DistributedMultiLock
慢慢迁移到新注解 - TODO 旧包中的
@DistributedMultiLock
需要实现@Lock4jMulti
- 旧包中的
@Cacheable
: 基于 Redis 的 SpringCache 集成- 扩展 key,支持 key 中自定义时间
abc#1000
- 扩展 key,支持 key 中自定义时间
YmRedisClient
: Redis 操作工具类,基于StringRedisTemplate
YmCacheClient
: Redis 缓存操作类YmIdempotentManager
: 幂等管理类YmRateLimitManager
: 限流管理类YmRedisKeyUtils
: Redis key 构建工具YmRedisLockClient
: 基于 Redisson 的分布式锁工具YmRedisMessageListener
: Redis 消息监听类,实现该类就可以实现消息监听
快速使用
添加依赖
注意: qifu-saas-parent >= 1.0.14-SNAPSHOT
<dependency>
<groupId>com.yuanmeng.engine</groupId>
<artifactId>engine-starter-redis</artifactId>
</dependency>
使用工具
除了 spring-cache 和 RedisMessageListener 默认关闭之外,别的功能都是默认开启的,开箱即用
幂等工具使用
public class Usage {
// 生成token请使用:idempotentManager.token("token");
@YmIdempotent(key = "#token", type = YmIdempotentType.TOKEN)
public String tokenTest(String token) {
log.info("tokenTest:{}", token);
return "success";
}
}
限流工具使用
public class Usage {
@YmRateLimit(key = "#id", timeUnit = RateIntervalUnit.HOURS, count = 5)
public String rateLimitHour(String id) {
log.info("rateLimitHour: {}", id);
return "success";
}
}
分布式锁工具使用
public class Usage {
@Autowired
private LockTemplate lockTemplate;
@Lock4j(name = "lock", keys = "#key", acquireTimeout = 1000)
public String lockAcquireFail(String key) throws InterruptedException {
log.info("lockAcquireFail: {}", key);
Thread.sleep(3000);
return "success";
}
// 多锁模式一
@Lock4jMulti(name = "lockMulti", keyList = "#keys")
public String lockMulti(List<String> keys) throws InterruptedException {
log.info("lockMulti: {}", key);
Thread.sleep(3000);
return "success";
}
// 多锁模式二
@Lock4jMulti(
value = {
@Lock4j(name = "lock", keys = "#key"),
@Lock4j(name = "lock", keys = "#key + '_123'")
}
)
public String lockMultiSecond(String key) throws InterruptedException {
log.info("lockMultiSecond: {}", key);
Thread.sleep(3000);
return "success";
}
public String lockTemplate(String key) throws InterruptedException {
LockInfo lock = lockTemplate.lock(key);
if (lock == null) {
throw new RuntimeException("lock fail");
}
try {
log.info("lockTemplate: {}", key);
Thread.sleep(2000);
} finally {
lockTemplate.releaseLock(lock);
}
return "success";
}
}
缓存工具使用
开启Redis缓存
yuanmeng:
redis:
cache-manager:
enable: true
使用缓存工具
public class Usage {
@Cacheable(cacheNames = "cache:cacheByUser", key = "#user.id + '#' + #expires")
public User cacheByIdWithExpires(User user, Integer expires) throws InterruptedException {
Thread.sleep(5000);
log.info("执行了cacheByIdWithExpires方法: {}, expires = {}", user.getId(), expires);
return user;
}
}
RedisMessageListener工具使用
开启Redis消息
yuanmeng:
redis:
redis-message-listener:
enable: true
使用消息工具
@Slf4j
@Component
public class RedisMessageListenerUsage extends YmRedisMessageListener {
public static final String TOPIC = "test_topic";
@Override
public String topic() {
return TOPIC;
}
@Override
public boolean enable() {
return true;
}
@Override
protected void onMessage(YmPublishWrapper messageBody, Message message, byte[] pattern) {
log.info("接收消息: {}", JSONObject.toJSONString(messageBody));
log.info("接收消息体:{}", JSONObject.toJSONString(messageBody.getData()));
log.info("接收消息体类型:{}", JSONObject.toJSONString(messageBody.getData().getClass().getName()));
log.info("接收消息Pattern:{}", new String(pattern));
}
}
完整默认配置文件
yuanmeng:
redis:
#- 开启 engine-redis-starter
enable: true
#- 开启全局默认前缀
enable-prefix: false
#- 全局默认前缀
prefix: sample
#- 开启 YmRedisClient 工具类
enable-redis-client: true
#- Redisson 相关配置
redisson:
enable: true
enable-codec: true
enable-prefix: false
prefix: sample_redisson
#- RedisMessageListener 相关配置
redis-message-listener:
enable: false
core-pool-size: 5
max-pool-size: 10
queue-capacity: 1000
listeners:
#- - com.yuanmeng.sample.starter.redis.message.RedisMessageListenerUsage
#- Spring Redis 缓存配置
cache-manager:
enable: false
default-cache-seconds: 3600
default-cache-null-seconds: 600
cache-seconds-map:
#- '[cache:simpleDefaultExpires]': 5000
cache-null-seconds-map:
#- '[cacheNull:simpleDefaultExpires]': 1200
#- 幂等配置
idempotent:
enable: true
enable-prefix: true
prefix: idempotent
#- 限流配置
rate-limit:
enable: true
enable-prefix: true
prefix: rate_limit
#- Lock4j Redisson 分布式锁配置
lock4j-redisson:
enable: false