redis单机版
springboot版本:2.3.6.release
依赖
<!-- redis依赖 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>
<!-- commons-pool2连接池 -->
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-pool2</artifactid>
</dependency>
application.yml 配置信息
spring:
# redis 配置
redis:
# 地址
host: localhost
# 端口,默认为6379
port: 6379
# 数据库索引
database: 0
# 密码
password:
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
redis配置文件
import com.fasterxml.jackson.annotation.jsonautodetect;
import com.fasterxml.jackson.annotation.jsontypeinfo;
import com.fasterxml.jackson.annotation.propertyaccessor;
import com.fasterxml.jackson.databind.objectmapper;
import com.fasterxml.jackson.databind.jsontype.impl.laissezfairesubtypevalidator;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.data.redis.connection.redisconnectionfactory;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.listener.redismessagelistenercontainer;
import org.springframework.data.redis.serializer.jackson2jsonredisserializer;
import org.springframework.data.redis.serializer.stringredisserializer;
/**
* redis序列化规则配置类
*/
@configuration
public class redisconfig {
/**
* 自定义序列化机制
*
* @param connectionfactory
* @return
*/
@bean
@suppresswarnings(value = {"unchecked", "rawtypes"})
public redistemplate<string, object> redistemplate(redisconnectionfactory connectionfactory) {
redistemplate<string, object> template = new redistemplate<string, object>();
template.setconnectionfactory(connectionfactory);
// json序列化配置
jackson2jsonredisserializer jackson2jsonredisserializer = new jackson2jsonredisserializer(object.class);
objectmapper objectmapper = new objectmapper();
objectmapper.setvisibility(propertyaccessor.all, jsonautodetect.visibility.any);
objectmapper.activatedefaulttyping(laissezfairesubtypevalidator.instance, objectmapper.defaulttyping.non_final, jsontypeinfo.as.property);
jackson2jsonredisserializer.setobjectmapper(objectmapper);
// string 的序列化
stringredisserializer stringredisserializer = new stringredisserializer();
// key采用string的序列化方式
template.setkeyserializer(stringredisserializer);
// hash的key也采用string的序列化方式
template.sethashkeyserializer(stringredisserializer);
// value序列化方式采用jackson
template.setvalueserializer(jackson2jsonredisserializer);
// hash的value序列化方式采用jackson
template.sethashvalueserializer(jackson2jsonredisserializer);
template.afterpropertiesset();
return template;
}
// redis 监听事件
@bean
redismessagelistenercontainer container(redisconnectionfactory connectionfactory) {
redismessagelistenercontainer container = new redismessagelistenercontainer();
container.setconnectionfactory(connectionfactory);
return container;
}
}
redis监听配置信息
import lombok.extern.slf4j.slf4j;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.listener.keyexpirationeventmessagelistener;
import org.springframework.data.redis.listener.redismessagelistenercontainer;
import org.springframework.stereotype.component;
@component
@slf4j
public class rediskeyexpirationlistener extends keyexpirationeventmessagelistener {
public rediskeyexpirationlistener(redismessagelistenercontainer listenercontainer) {
super(listenercontainer);
log.info("过期事件,启动监听......");
}
/**
* redis失效事件 key
*
* @param message
* @param pattern
*/
@override
public void onmessage(message message, byte[] pattern) {
string expirekey = message.tostring();
system.out.println("过期事件监听键:" + expirekey);
// 获取订单编号
if (expirekey.startswith("order:")) {
// 截取订单编号
final string str = expirekey.substring(expirekey.lastindexof(":") + 1);
system.out.println(str);
system.out.println("转换后订单编号:" + long.valueof(str));
// todo 业务处理逻辑
}
}
}
打开 redis 客户端添加 key:setex "order:20220824170501" 20 "20220824170501" 20秒后过期
查看控制台输出内容:

到此这篇关于springboot使用redis单机版过期键监听事件的实现示例的文章就介绍到这了,更多相关springboot redis过期键监听内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论