In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use the automatic expiration mechanism in Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Automatic expiration mechanism in Redis
Meet the requirements: automatically cancel the expired order, for example, automatically change the status of the order without payment for 30 minutes
1. Automatically expire departure event notification using Redis Key
two。 Check after using scheduled tasks for 30 minutes
3. Check according to the rotation training every minute.
CREATE TABLE `order_ number` (`id`int (11) NOT NULL AUTO_INCREMENT, `order_ name` varchar (255DEFAULT NULL), `order_ status` int (11) DEFAULT NULL, `order_ token` varchar (255DEFAULT NULL), `order_ id` DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8; 1. Use Redis Key automatic expiration mechanism
When our key fails, we can execute our client callback listening method.
You need to configure it in Redis:
1. Open the redis.conf configuration file
Vi redis.conf
two。 Look for notify-keyspace-events in the configuration file
/ notify-keyspace-events
3. Change to notify-keyspace-events Ex
4. Restart redis
2. SpringBoot integrates key failure monitoring @ Configurationpublic class RedisListenerConfig {@ Bean RedisMessageListenerContainer container (RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer (); container.setConnectionFactory (connectionFactory); return container;}} @ Componentpublic class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {public RedisKeyExpirationListener (RedisMessageListenerContainer listenerContainer) {super (listenerContainer);} @ Resource private OrderMapper orderMapper; / * * to be paid * / private static final Integer ORDER_STAYPAY = 0 / * fail * / private static final Integer ORDER_INVALID = 2; / * use this method to monitor the execution of this method when our key fails * * @ param message * @ param pattern * / @ Override public void onMessage (Message message, byte [] pattern) {String expiraKey = message.toString () System.out.println ("the key:expiraKey:" + expiraKey + "invalid ~"); / / prefix judgment orderToken OrderEntity orderNumber = orderMapper.getOrderNumber (expiraKey); if (orderNumber = = null) {return;} / / get order status Integer orderStatus = orderNumber.getOrderStatus () / / if the order status is pending payment, directly modify the order to if (orderStatus.equals (ORDER_STAYPAY)) {orderMapper.updateOrderStatus (expiraKey, ORDER_INVALID); / / inventory plus 1}} @ RestControllerpublic class MemberController {@ Autowired private UserMapper userMapper / * * @ return * / @ RequestMapping ("/ getListMember") @ Cacheable (cacheNames = "member", key = "'getListMember'") public List getListMember () {return userMapper.findMemberAll ();}} @ Datapublic class OrderEntity {private Long id; private String orderName; / * 0 to be paid * / 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;}} public interface OrderMapper {@ Insert ("insert into order_number values (null,# {orderName}, 0Jing # {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) }
1. Access the addOrder interface
two。 View database data
3. Redis expires after 10 seconds, and callback mechanism is executed.
4. Check the database again and the status has been modified
This is the end of the content of "how to use the automatic expiration mechanism in Redis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.