Redis监听key的失效事件
概念:
当redis的key失效以后,会回调程序的监听方法。
应用场景:
1. 一个交易系统里面有一个价格提醒的功能,用户可以设置一组价格并设置一个周期,后台需要在交易的时间内进行价格扫描一旦触发用户设置的价格的周期就需要下发消息提醒给用户,提醒用户交易做单;
2. 订单超时自动关闭功能
现在我们就来实现订单超时自动关闭功能。
实现原理:
- 生成订单的时候,给redis中存入key加上过期时间,且订单状态为未支付0
- 过期时间到了,redis配置了key失效时间的回调,我们取出回调中的key,判断一下是否为订单的key
- 如果是则根据key取出订单的id,再判断订单的状态
- 如果订单的状态为未支付,则更新数据库,关闭订单
redis的key失效监听默认是没有开启的,需要我们手动开启。
vi redis.conf
将notify-keyspace-events "" 改为notify-keyspace-events "Ex"
然后重启redis服务即可。
SpringBoot整合redis实现订单超时关闭
RedisListenerConfig
@Configuration
public class RedisListenerConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}
RedisKeyExpirationListener key失效的监听类
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
/**
* 待支付
*/
private static final Integer ORDER_STAYPAY = 0;
/**
* 失效
*/
private static final Integer ORDER_INVALID = 2;
@Autowired
private OrderMapper orderMapper;
/**
* Redis失效事件 key
*
* @param message
* @param pattern
*/
@Override
public void onMessage(Message message, byte[] pattern) {
String expiraKey = message.toString();
// 根据key查询 value 如果还还是为待支付状态 将订单改为已经超时~~
OrderEntity orderNumber = orderMapper.getOrderNumber(expiraKey);
System.out.println(expiraKey);
if (orderNumber == null) {
return;
}
if (orderNumber.getOrderStatus().equals(ORDER_STAYPAY)) {
// 将订单状态改为已经失效
orderMapper.updateOrderStatus(expiraKey, ORDER_INVALID);
}
}
}
OrderController
@RestController
public class OrderController {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RedisUtils redisUtils;
@RequestMapping("/saveOrder")
public String saveOrder() {
// 1.生成token
String orderToken = UUID.randomUUID().toString();
String orderId = System.currentTimeMillis() + "";
//2. 将该token存放到redis中
redisUtils.setString(orderToken, orderId, 5L);
OrderEntity orderEntity = new OrderEntity(null, "蚂蚁课堂永久会员", orderId, orderToken);
int result = orderMapper.insertOrder(orderEntity);
return result > 0 ? "success" : "fail";
}
}
OrderEntity
@Data
public class OrderEntity {
private Long id;
private String orderName;
/**
* 0 待支付 1 已经支付
*/
private Integer orderStatus;
private String orderToken;
private String orderId;
public OrderEntity(Long id, String orderName, String orderId, String orderToken) {
this.id = id;
this.orderName = orderName;
this.orderId = orderId;
this.orderToken = orderToken;
}
}
OrderMapper
public interface OrderMapper {
@Insert("insert into order_number values (null,#{orderName},0,#{orderToken},#{orderId})")
int insertOrder(OrderEntity OrderEntity);
@Select("SELECT ID AS ID ,order_name AS ORDERNAME ,order_status AS orderstatus,order_token as ordertoken,order_id as orderid FROM order_number\n" +
"where order_token=#{orderToken};")
OrderEntity getOrderNumber(String orderToken);
@Update("\n" +
"\n" +
"update order_number set order_status=#{orderStatus} where order_token=#{orderToken};")
int updateOrderStatus(String orderToken, Integer orderStatus);
}