需求
在做有时效业务需求的时候, 比如我发起一个签到, 2小时内有效,过期就不能再签到了.
做法
- 读取数据的时候判断结束时间是都小于当前时间, 是就过期, 不是就有效
- 设置定时任务, 让任务去做这个事情, 我们只需要读取状态就可以了,
- 结合缓存Redis, 既能加快速度, 还有key失效的监听.
这里要描述Redis键失效的监听
代码
- 修改Redis server的配置(redis.conf)
默认是: notify-keyspace-events "",修改成 notify-keyspace-events Ex;
配置Ex对应的意思如下: E: 键事件通知,以__keysevent@<db>__
为前缀 x:过期事件(每次key过期时生成)
# K 键空间通知,以__keyspace@<db>__为前缀
# E 键事件通知,以__keysevent@<db>__为前缀
# g del , expipre , rename 等类型无关的通用命令的通知, ...
# $ String命令
# l List命令
# s Set命令
# h Hash命令
# z 有序集合命令
# x 过期事件(每次key过期时生成)
# e 驱逐事件(当key在内存满了被清除时生成)
# A g$lshzxe的别名,因此”AKE”意味着所有的事件
- 编写监听
public class MyKeyExpirationEventMessageListener extends KeyExpirationEventMessageListener{
public MyKeyExpirationEventMessageListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Override
public void onMessage(Message message, byte[] pattern) {
// System.out.println(new String(message.getBody()));
// System.out.println(new String(message.getChannel()));
// System.out.println(new String(pattern));
// super.onMessage(message, pattern);
}
}
- SpringBoot 配置
package com.guangyiedu.clazz.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import com.guangyiedu.clazz.service.cache.MyKeyExpirationEventMessageListener;
import redis.clients.jedis.JedisPoolConfig;
/**
* redis的配置
*
* @author atrun
*
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
@SuppressWarnings("rawtypes")
public CacheManager cacheManager(RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}
@Bean
@ConfigurationProperties(prefix = "spring.redis.pool")
public JedisPoolConfig getRedisConfig() {
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
}
@Bean
public StringRedisTemplate redisTemplate() {
StringRedisTemplate template = new StringRedisTemplate(getConnectionFactory());
return template;
}
@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(){
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(getConnectionFactory());
return container;
}
@Bean
public MyKeyExpirationEventMessageListener myKeyExpirationEventMessageListener (){
MyKeyExpirationEventMessageListener listener = new MyKeyExpirationEventMessageListener(redisMessageListenerContainer());
return listener;
}
}