Redis Key Expiration Time monitoring

Source: Internet
Author: User
Tags redis

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);

});


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.