赞
踩
@Configuration
public class RedisListenerConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
String uploadKey = "这是自定义的key_LANSHAN";
RedisClient redisClient = SpringUtils.getBean(RedisClient.class);
//插入缓存
redisClient.set(uploadKey,"123456");
//设置缓存过期时间为10秒
redisClient.expire(uploadKey, 10);
@Component public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { @Override public void onMessage(Message message, byte[] pattern) { try{ String expiredKey = message.toString();//获取所有key //以下是你想执行的代码逻辑 if(expiredKey.endsWith("_LANSHAN")){//判断后缀是否为'_LANSHAN' System.err.print("我得到的key:"+expiredKey); } }catch (Exception e){ e.printStackTrace(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。