本篇文章小编给大家分享一下Redis实现订单自动过期功能代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
配置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)方法。
创造与魔法 安卓版v1.0.0750
创造与魔法是一款开放世界手游,在游戏中玩家可探索这个奇妙的世
创造与魔法修改版 最新版v1.0.0750
创造与魔法无限点券版是款探索冒险游戏,该款游戏的操作还是蛮自
战争与文明官方版本 安卓版v1.7.16
战争与文明是一款由上海邮通科技有限公司开发的战争策略游戏,这
迷你世界0元领皮肤无限迷你币版 最新安卓版v1.43.0
迷你世界0元购买皮肤版是这款开放沙盒冒险建造游戏的特殊破解版
创造与魔法无限经验版 安卓版v1.0.0750
创造与魔法无限经验版是款可以改造环境,整个游戏的自由度还是蛮