The first step is to modify the configuration file
############################# EVENT NOTIFICATION ##############################
# Redis can notify Pub/sub clients about events happening in the key space.
# This feature was documented at Http://redis.io/topics/notifications
#
# for instance if Keyspace events notification is enabled, and a client
# performs a DEL operation on key ' foo ' stored in the Database 0, two
# messages'll be published via Pub/sub:
#
# PUBLISH __keyspace@0__:foo del
# PUBLISH __keyevent@0__:del foo
#
# It is possible to select the events that Redis would notify among a set
# of classes. Every class is identified by a single character:
#
# K Keyspace events, published with __keyspace@<db>__ prefix.
# E KeyEvent events, published with __keyevent@<db>__ prefix.
# g Generic Commands (Non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String Commands
# l List Commands
# s Set commands
# h Hash Commands
# Z Sorted Set commands
# x Expired Events (events generated every time a key expires)
# e evicted events (events generated when a key was evicted for MaxMemory)
# A Alias for G$lshzxe, so the ' ake ' string means all the events.
#
# the "Notify-keyspace-events" takes as argument a string is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# example:to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events ELG
#
# Example 2:to Get the stream of the expired keys subscribing to channel
# Name __keyevent@0__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don ' t need
# This feature and the feature has some overhead. Note this if you don ' t
# Specify at least one of K or E, no events would be delivered.
#notify-keyspace-events ""
Notify-keyspace-events Ex
The second step is to restart the Redis service.
The third step, using the Publish subscription mechanism, the following code is based on Stackexchange.redis
var redisclient = Waterredisclient.getwaterclientmanager ();//Object pool gets a Redis client
var subscriber = RedisClient.Redis.GetSubscriber ();
Subscriber. Subscribe ("__keyevent@0__:expired", (channel, NotificationType) =>
{
Console.WriteLine ("Key Expired" +channel + "| | | | |" + notificationtype);
});