When performing certain timed operations, we need feedback on the results. For example, orders are automatically deleted after expiration, orders are automatically praised after a few days, unpaid orders are closed after 15 minutes, etc., which can be completed using some timed task frameworks or polling methods. This article uses the Redis advanced feature cache expiration event notification mechanism combined with .NET/C# code to complete business requirements.
introduction
Usage scenarios: 1. In payment business, unpaid orders are automatically closed 2. Cache expiration reminders
In general, we can use the timing service to process the automatic closure of unpaid orders, such as calling the interface every minute to process unpaid and expired orders, but in this case, it will consume computer performance, even if there is no order, it will be processed every minute, and the maximum delay of the order processing time will be 59s, and the timing service must be always available
So what do we want to do only when there are unpaid and expired orders with low latency processing, we can use Redis' cache expiration mechanism to push the subscription.
Redis subscription
Modify the notify-keyspace-events Ex in the configuration file redis.conf
In order to save cup resources, event notifications are not turned on by default, and the default is notify-keyspace-events
# K key space notification, <db>prefixed with __keyspace@__ # E key event notification, <db>prefixed with __keysevent@__ # g del, expipre, rename and other types of generic commands, ... # $string command # l List command #s Set command # h Hash command # z ordered set command # x Expiration event (generated every time a key expires) # e eviction event (generated when the key is cleared when the memory is full) # A g$lshzxe alias, so "AKE" means all events
notify-keyspace-events "kx" indicates that you want to monitor for invalidation events for a key. Setting the parameter to the string AKE means sending all types of notifications.
I installed redis as a service on my machine, modified the redis.windows.conf configuration file, and needed to restart the redis service configuration to take effect.
.NET/C# code example
The package is as follows:
invoke
Let's try writing a cache with redis-cli.
In fact, you can also subscribe to cache expiration notifications by executing the following command through redis-cli:
When N clients subscribe to cache expiration event notifications, redis will notify N users of the same expiration message, not just one of the clients, as shown in the figure below:
C# source code download:
https://down.itsvse.com/item/17856.html
summary
Redis pub/sub is an unreliable message mechanism, it will not store information, it will only forward online, and there is definitely no ack confirmation mechanism, and only the subscription segment will be forwarded, so Keyspace Notifications is also an unreliable notification system, if our business needs good reliability, then this is not the best choice. Generally, we recommend RabbitMQ's DLX (Dead-Letter-Exchange) to implement it, which is the delayed queue function. It's just that Redis' solution is easier to implement and less expensive to operate. It is still very convenient for businesses that do not require high reliability. |