Redis实现订单自动过期功能

Redis实现订单自动过期功能

前言

用户下单后,规定XX分钟后自动设置为“已过期”,不能再发起支付。项目类似此类"过期"的需求,笔者提供一种使用Redis的解决思路,结合Redis的订阅、发布和键空间通知机制(Keyspace Notifications)进行实现。

配置redis.confg

notify-keyspace-events选项默认是不启用,改为notify-keyspace-events “Ex”。重启生效,索引位i的库,每当有过期的元素被删除时,向**keyspace@:expired**频道发送通知。
E表示键事件通知,所有通知以__keyevent@__:expired为前缀;
x表示过期事件,每当有过期被删除时发送。

与SpringBoot进行集成

①注册JedisConnectionFactory
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.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {
	
	@Value("${redis.pool.maxTotal}")
	private Integer maxTotal;
	
	@Value("${redis.pool.minIdle}")
	private Integer minIdle;
	
	@Value("${redis.pool.maxIdle}")
	private Integer maxIdle;
	
	@Value("${redis.pool.maxWaitMillis}")
	private Integer maxWaitMillis;
	
	@Value("${redis.url}")
	private String redisUrl;
	
	@Value("${redis.port}")
	private Integer redisPort;
	
	@Value("${redis.timeout}")
	private Integer redisTimeout;
	
	@Value("${redis.password}")
	private String redisPassword;
	
	@Value("${redis.db.payment}")
	private Integer paymentDataBase;
	
	private JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(maxTotal);
		config.setMinIdle(minIdle);
		config.setMaxIdle(maxIdle);
		config.setMaxWaitMillis(maxWaitMillis);
		return config;
	}
	
	@Bean
	public JedisPool jedisPool() {
		JedisPoolConfig config = this.jedisPoolConfig();
		JedisPool jedisPool = new JedisPool(config, redisUrl, redisPort, redisTimeout, redisPassword);
		return jedisPool;
	}
	
	@Bean(name = "jedisConnectionFactory")
	public JedisConnectionFactory jedisConnectionFactory() {
		RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
		redisStandaloneConfiguration.setDatabase(paymentDataBase);
		redisStandaloneConfiguration.setHostName(redisUrl);
		redisStandaloneConfiguration.setPassword(RedisPassword.of(redisPassword));
		redisStandaloneConfiguration.setPort(redisPort);

		return new JedisConnectionFactory(redisStandaloneConfiguration);
	}
}
②注册监听器
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service(value ="paymentListener")
public class PaymentListener implements MessageListener {

	@Override
	@Transactional
	public void onMessage(Message message, byte[] pattern) {
		// 过期事件处理流程
	}

}
③配置订阅对象
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
@AutoConfigureAfter(value = RedisConfig.class)
public class PaymentListenerConfig {
	
	@Autowired
	@Qualifier(value = "paymentListener")
	private PaymentListener paymentListener;
	
	@Autowired
	@Qualifier(value = "paymentListener")
	private JedisConnectionFactory connectionFactory;
	
	@Value("${redis.db.payment}")
	private Integer paymentDataBase;
	
	@Bean
	RedisMessageListenerContainer redisMessageListenerContainer(MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 监听paymentDataBase 库的过期事件
        String subscribeChannel = "__keyevent@" + paymentDataBase + "__:expired";
        container.addMessageListener(listenerAdapter, new PatternTopic(subscribeChannel));
        return container;
	}
	
	@Bean
    MessageListenerAdapter listenerAdapter() {
        return new MessageListenerAdapter(paymentListener);
    }
}

paymentDataBase 库元素过期后就会跳入PaymentListener 的onMessage(Message message, byte[] pattern)方法。

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Redis缓存可以通过设置键的过期时间来实现订单自动取消功能。当订单生成时,我们将订单信息存储到Redis缓存中,并设置一个过期时间(比如30分钟)。这样,订单信息将在30分钟后自动Redis中移除。如果订单在30分钟内完成支付,我们可以根据订单号在Redis中查找订单信息并进行相应处理。如果订单在30分钟内未完成支付,则订单信息会在过期时间到达时自动Redis中移除,并触发订单取消逻辑。 在实现过程中,可以使用Redis的SET命令将订单信息保存到Redis中,并通过EXPIRE命令设置该键的过期时间。例如: ``` SET order:12345 "待支付订单信息" EXPIRE order:12345 1800 ``` 以上代码将"待支付订单信息"保存在键名为"order:12345"的键中,并设置其过期时间为1800秒(30分钟)。当订单完成支付时,可以通过GET命令获取订单信息。如果返回空值,则说明订单已被取消。如果返回订单信息,则可以继续处理。 另外,为了保证系统的可靠性,可以在订单取消时触发一些额外的操作,如发送通知给用户或其他相关系统。可以使用Redis的发布/订阅功能,将订单取消事件发布到指定的频道,然后其他订阅该频道的系统可以接收到该事件并执行相应的处理逻辑。 总之,通过使用Redis的缓存功能,可以轻松实现订单自动取消的功能,并且具备一定的灵活性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值