In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how redis monitors failed key. I hope you will learn a lot after reading this article. Let's discuss it together.
First, let's look at one question:
How to deal with automatic cancellation when an order expires, such as automatically changing the status of an order without payment within 30 minutes of placing an order?
Solution:
You can use redis's natural key automatic expiration mechanism to write the order id to redis when placing an order. The expiration time is 30 minutes, and the order status is checked after 30 minutes. If it is not paid, it will be processed. But is there a notice in redis when the key expires? The answer is yes.
Turn on redis key expiration reminder
Modify the configuration of redis-related events. Find the redis configuration file redis.conf, and view the configuration item of "notify-keyspace-events". If not, add "notify-keyspace-events Ex". If there is a value, add Ex. The relevant parameters are described as follows:
K:keyspace event, which is published with _ _ keyspace@__ prefix; E:keyevent event, which is published with _ _ keyevent@__ prefix; g: general, non-specific type of command, such as del,expire,rename, etc.; $: string-specific command; l: list-specific command; s: collection-specific command; h: hash command Z: ordered collection of specific commands; x: expiration event, which occurs when a key expires and is deleted; e: eviction event, which occurs when a key is deleted due to maxmemore policy; alias for A:g$lshzxe, so "AKE" means all events.
Redis Test:
Open a redis-cli to monitor db0's key expiration events
127.0.0.1 6379 > PSUBSCRIBE _ _ keyevent@0__:expiredReading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "_ _ keyevent@0__:expired" 3) (integer) 1
Open another redis-cli and send a timed expired key
127.0.0.1 6379 > setex test_key 3 test_value
If you look at the previous redis-cli, you will find that you have received an expired keytest_key, but you cannot receive an expired value test_value
127.0.0.1 6379 > PSUBSCRIBE _ _ keyevent@0__:expiredReading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "_ keyevent@0__:expired" 3) (integer) 11) "pmessage" 2) "_ keyevent@0__:expired" 3) "_ keyevent@0__:expired" 4) "test_key"
Use in springboot
Add dependencies to pom
Org.springframework.boot spring-boot-starter-data-redis
Define configuration RedisListenerConfig
Import edu.zut.ding.listener.RedisExpiredListener;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.PatternTopic;import org.springframework.data.redis.listener.RedisMessageListenerContainer;/** * @ Author lsm * @ Date 20:56 on 2018-10-27 * / @ Configurationpublic class RedisListenerConfig {@ Bean RedisMessageListenerContainer container (RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer () Container.setConnectionFactory (connectionFactory); / / container.addMessageListener (new RedisExpiredListener (), new PatternTopic ("_ keyevent@0__:expired")); return container;}}
Define listeners, implement the KeyExpirationEventMessageListener interface, view source code discovery, and this interface listens for all db expiration events keyevent@*:expired "
Import edu.zut.ding.constants.SystemConstant;import edu.zut.ding.enums.OrderState;import edu.zut.ding.service.OrderService;import org.springframework.beans.factory.annotation.Autowired;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 / * listen for expired events of all db _ keyevent@*__:expired "* @ author lsm * / @ Componentpublic class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {public RedisKeyExpirationListener (RedisMessageListenerContainer listenerContainer) {super (listenerContainer) } / * * for redis data failure events, perform data processing * @ param message * @ param pattern * / @ Override public void onMessage (Message message, byte [] pattern) {/ / users can do their own business processing. Note that message.toString () can get invalid key String expiredKey = message.toString (). If (expiredKey.startsWith ("Order:")) {/ / if it is the key at the beginning of Order:, process}
Or open container.addMessageListener (new RedisExpiredListener (), new PatternTopic ("_ _ keyevent@0__:expired")) in RedisListenerConfig; comment, and then define a listener to monitor the _ _ keyevent@0__:expired event, that is, the db0 expiration event. The definition of this place is flexible, and you can define what events to monitor on your own.
Import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;/** * @ author lsm * / public class RedisExpiredListener implements MessageListener {/ * client listens to the subscription topic and triggers this method when there is a message; * you can't get value, only key. * for the time being, if the redis service notifies the java service that a key has expired when the key expires (or after the expiration), it is impossible to get the redis-value corresponding to this redis-key in java. * * solution: * create a copy/shadow key, such as set vkey "vergilyn"; corresponding to copykey: set copykey:vkey "" ex 10; * the real key is "vkey" (used in business), and the failure trigger key is "copykey:vkey" (its value is an empty character to reduce memory space consumption). * when the trigger of "copykey:vkey" fails, the value of failure is obtained from "vkey", and after logical processing, "del vkey" * * defect: * 1: there is excess key. (copykey/shadowkey) * 2: not rigorous. Suppose the copykey expires at 12:00:00 and the notification is received at 12:10:00. If the program modifies the key in the 10min at this interval, you will not get the value at the time of failure. * (point 1 has little impact The second point seems that the Pub/Sub of redis itself is not rigorous, and there are still modifications of value after failure, which should be eliminated in design / logic) * when "copykey:vkey" triggers failure, the value of failure is obtained from "vkey", and after logical processing, "del vkey" * * / @ Override public void onMessage (Message message, byte [] bytes) {byte [] body = message.getBody () / / recommended: valueSerializer byte [] channel = message.getChannel (); System.out.print ("onMessage > >"); System.out.println ("channel:% s, body:% s, bytes:% s", new String (channel), new String (body), new String (bytes) }} after reading this article, I believe you have a certain understanding of how redis monitors failed key. You want to know more about it. Welcome to follow the industry information channel. Thank you for your reading!
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.