Key过期事件的Redis配置
notify-keyspace-events Ex
notify-keyspace-events 的参数为 “Ex”。x 代表了过期事件
ok 重启redis
配置文件
spring:
redis:
order:
database: 2
host: 47.112.111.131
port: 30004 # 密码(默认为空)
maven
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
代码
package com.eaton.guan.club.engine.orderRedis;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.*;
import redis.clients.jedis.JedisPoolConfig;
/**
* redis配置类
*
**/
@Configuration
public class RedisConfig {
@Value("${spring.redis.order.host}")
private String userHost;
@Value("${spring.redis.order.port}")
private String userPort;
@Value("${spring.redis.order.database}")
private Integer database;
/**
* 最大空闲连接数
*/
private static final int MAX_IDLE = 200;
/**
* 最大连接数
*/
private static final int MAX_TOTAL = 1024;
/**
* 建立连接最长等待时间
*/
private static final long MAX_WAIT_MILLIS = 10000;
/**
* 配置工厂
* @param host redis ip
* @param port redis 端口
* @param password redis 密码
* @param maxIdle 最大空闲连接数
* @param maxTotal 最大连接数
* @param maxWaitMillis 建立连接最长等待时间
* @param index redis 库
* @return
*/
public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle,
int maxTotal, long maxWaitMillis, int index) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(port);
if (!StringUtils.isEmpty(password)) {
jedisConnectionFactory.setPassword(password);
}
if (index != 0) {
jedisConnectionFactory.setDatabase(index);
}
jedisConnectionFactory.setPoolConfig(poolConfig(maxIdle, maxTotal, maxWaitMillis, false));
jedisConnectionFactory.afterPropertiesSet();
return jedisConnectionFactory;
}
//连接池配置
public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxTotal(maxTotal);
poolConfig.setMaxWaitMillis(maxWaitMillis);
poolConfig.setTestOnBorrow(testOnBorrow);
return poolConfig;
}
@Bean(name = "redisOrderTemplate")
public StringRedisTemplate redisOrderTemplate() {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(
connectionFactory(userHost, Integer.parseInt(userPort), null, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, database));
return template;
}
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> userUserTemplate) {
return userUserTemplate.opsForValue();
}
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
redis key过期消费类
package com.eaton.guan.club.engine.orderRedis;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
* 主要作用就是:接收过期的redis消息,获取到key,key就是订单号,然后去更新订单号的状态(说明一下:用户30分钟不支付的话取消用户的订单)
*/
@Transactional
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
/**
*
* @param message redis key
* @param pattern
*/
@Override
public void onMessage(Message message, byte[] pattern) {
String orderCode = message.toString();
// if (!StringUtils.isBlank(orderCode)) {
//
// 去数据库把订单状态改成过期,这里是我们的业务逻辑
KcTeaRoomConsumeRecord kcTeaRoomConsumeRecord = new KcTeaRoomConsumeRecord();
kcTeaRoomConsumeRecord.setOrderStatus(5); // 5表示订单状态是过期
kcTeaRoomConsumeRecord.setOrderCode(orderCode);
kcTeaRoomConsumeRecordDAOMapper.updateKcTeaRoomConsumeRecordByCondition(kcTeaRoomConsumeRecord);
// }
System.out.println("过期的订单号是: " + orderCode);
}
}
注册redisTemplate,作为消息队列的发布者
package com.eaton.guan.club.engine.orderRedis;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
* 注册redisTemplate,作为消息队列的发布者
*/
@Configuration
public class RedisListenerConfig {
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
redis工具类
package com.eaton.guan.club.engine.orderRedis;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class RedisUtil {
/**
* 是否开启redis缓存 true开启 false关闭
*/
private boolean open = true;
@Autowired(required = false)
@Qualifier("redisOrderTemplate")
private StringRedisTemplate redisTemplate;
@Autowired(required = false)
private ValueOperations<String, String> valueOperations;
@Autowired(required = false)
private HashOperations<String, String, Object> hashOperations;
@Autowired(required = false)
private ListOperations<String, Object> listOperations;
@Autowired(required = false)
private SetOperations<String, Object> setOperations;
@Autowired(required = false)
private ZSetOperations<String, Object> zSetOperations;
/**
* 默认过期时长,单位:秒
*/
// public final static long DEFAULT_EXPIRE = 10;
/**
* 不设置过期时长
*/
public final static long NOT_EXPIRE = -1;
public final static Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create();
public boolean exists(String key) {
if (!open) {
return false;
}
return redisTemplate.hasKey(key);
}
public void set(String key, Object value, long expire) {
if (!open) {
return;
}
try {
valueOperations.set(key, toJson(value));
} catch (Exception e0) {
System.out.println(e0.getMessage());
}
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
}
public void set(String key, Object value) {
if (!open) {
return;
}
try {
valueOperations.set(key, toJson(value));
} catch (Exception e0) {
System.out.println(e0.getMessage());
}
}
public <T> T get(String key, Class<T> clazz, long expire) {
if (!open) {
return null;
}
String value = valueOperations.get(key);
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value == null ? null : fromJson(value, clazz);
}
public <T> T get(String key, Class<T> clazz) {
if (!open) {
return null;
}
return get(key, clazz, NOT_EXPIRE);
}
public String get(String key, long expire) {
if (!open) {
return null;
}
String value = valueOperations.get(key);
if (expire != NOT_EXPIRE) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
return value;
}
public String get(String key) {
if (!open) {
return null;
}
return get(key, NOT_EXPIRE);
}
public void delete(String key) {
if (!open) {
return;
}
if (exists(key)) {
redisTemplate.delete(key);
}
}
public void delete(String... keys) {
if (!open) {
return;
}
for (String key : keys) {
redisTemplate.delete(key);
}
}
public void deletePattern(String pattern) {
if (!open) {
return;
}
Set<String> keys = redisTemplate.keys(pattern);
if (keys.size() > 0) {
redisTemplate.delete(keys);
}
}
/**
* Object转成JSON数据
*/
private String toJson(Object object) {
if (!open) {
return null;
}
if (object instanceof Integer || object instanceof Long || object instanceof Float ||
object instanceof Double || object instanceof Boolean || object instanceof String) {
return String.valueOf(object);
}
return gson.toJson(object);
}
/**
* JSON数据,转成Object
*/
private <T> T fromJson(String json, Class<T> clazz) {
if (!open) {
return null;
}
return gson.fromJson(json, clazz);
}
}
Redis监听过期key并获取到value的解决方案
将你的key分为两个:key 和 key_2。
key 设置过期时间,key_2 存你的数据。
检测到key过期时,获取key_2的数据,然后删除key_2
key_2根据你的需求改变数据类型
redisTemplate.opsForValue().set("demo", "", 10, TimeUnit.SECONDS);
redisTemplate.opsForValue().set("demo" + "_2", job);
@Override
public void onMessage(Message message, byte[] pattern) {
// message.toString()可以获取失效的key
String expiredKey = message.toString();
String key = expiredKey + "_2";
//获取你存的值
Object o = redisTemplate.opsForValue().get(key);
//刪掉key的值
redisTemplate.delete(key);
System.err.println(expiredKey + "-value: " + o);
}
测试test
@GetMapping("/a")
public void test (){
redisKey ,redisVlue,过期时间
redisUtil.set("测试001", "测试002", 60);
}