Skip to content

Instantly share code, notes, and snippets.

@sayganov
Last active April 23, 2023 21:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Redis keyspace notifications
using StackExchange.Redis;
var configurationOptions = new ConfigurationOptions
{
EndPoints =
{
"localhost:6379"
}
};
var connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
var subscriber = connectionMultiplexer.GetSubscriber();
await subscriber.SubscribeAsync("__keyspace@0__:*", (channel, type) =>
{
var key = GetKey(channel);
switch (type)
{
case "set":
Console.WriteLine($"Set: {key}");
break;
case "expire":
Console.WriteLine($"Expire: {key}");
break;
case "expired":
Console.WriteLine($"Expired: {key}");
break;
}
});
Console.ReadLine();
await subscriber.UnsubscribeAllAsync();
await connectionMultiplexer.DisposeAsync();
static string GetKey(string channel)
{
var index = channel.IndexOf(':');
if (index >= 0 && index < channel.Length - 1)
{
return channel[(index + 1)..];
}
return channel;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment