This article is a mirror article of machine translation, please click here to jump to the original article.

View: 34045|Reply: 6

[Source] .NET uses Redis cache for expiration notifications

[Copy link]
Posted on 2020-8-22 10:34:59 | | | |
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.




Previous:.NET Core 3.1 Installation Chinese Smart Prompt
Next:.NET does not get the full reference assembly assembly
Posted on 2020-8-29 14:33:18 |
Learn to learn
Posted on 2021-11-30 13:16:47 |
Learn it
Posted on 2021-11-30 13:21:02 |
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.
Posted on 2022-2-14 17:42:43 |
Look, learn
Posted on 2022-3-2 15:25:00 |
Learn it
Posted on 2022-7-27 16:40:13 |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com