Winston Smith
Winston Smith

Reputation: 133

Redis pub/sub, not getting expected notifications

Context

I'm attempting to build a small prototype of Redis incorporated functionality into my already existing .net web application.

What is working

I have Redis installed, running, and can connect my application to it.

I have created a small class to connect to Redis and have successfully prototyped both setting and getting records to and from the running Redis instance.

What is not working

Where I'm having an issue is with subscribing to event notifications - specifically having the application informed whenever a key expires in Redis.

What I have done thus far

I have configured Redis to enable event notifications using "notify-keyspace-events Ex" in my redis.conf file.

In my class I have attempted to subscribe to keyevents for key expiration with the following:

using StackExchange.Redis;
...
namespace abc
{
   public static class RedisClass{
      private static readonly ILogger log;
      private static readonly int port;
      private static ConfigurationOptions RedisConfiguration => new ConfigurationOptions{
         EndPoints = {{"localhost", port}},
         DefaultDatabase = 0,
         ClientName = "redisClientName"
      };
      private static IConnectionMultiplexer redisMultiplexer => ConnectionMultiplexer.Connect(RedisConfiguration);
      
      static RedisClass(){
         port = ####;
         ConfigureSubscription();
      }

      private static void ConfigureSubscription {
         using (IConnectionMultiplexer connection = RedisMultiplexer)
         {
            IDatabase db = connection.GetDatabase();
            ISubscriber subscriber = connection.GetSubscriber();

            subscriber.Subscribe("__keyevent@0__:expired", (channel, value) =>
            {
                log.Error($"from subscription: {channel}, {value}");
            });

            log.Error($"subscription IsConnected : {subscriber.IsConnected()}");
            log.Error($"ping: {subscriber.Ping()}");
            
            //test publish
            subscriber.Publish("__keyevent@0__:expired", new RedisValue("testing publish"));
         }
      log.Error("subscription set up");
      }
   }
}

I will open a command window to show all subscription messages by entering:

redis-cli -p #### --csv psubscribe '*'

I will then run the app, and observe the following in the log file:

...|ERROR|subscription IsConnected: True

...|ping: 00:00:00.0009082

...|subscription set up

...|from subscription: keyevent@0:expired, testing publish

and observe the following in the command window:

"pmessage","*","keyevent@0:expired","testing publish"

So far so good.

I will then open up a second command window, and in this second command window create a key in Redis, and have it expire:

set keyToExpire valueToExpire EX 5

The first command window diplays the expiration message, as expected

"pmessage","*","keyevent@0:expired","keyToExpire"

But my log file does not show a corresponding "from subscription" message.

I've looked through a handful of pub/sub examples. They all seem to be following the same general approach I have captured above.

My Questions

Is there something I'm missing? I'm expecting to see the second "from subscription" message in my log file.

Is there some other away to have my application be notified whenever a key expires from Redis?

Upvotes: 1

Views: 1295

Answers (1)

Winston Smith
Winston Smith

Reputation: 133

I got this worked out.

There were two issues with my initial approach.

  1. The command line redis-cli instances were not on the same ClientName as my application. I was able ton configure the command line redis-cli instances with the CLIENT SETNAME command.
  2. The using statement in the application ConfigureSubscription method meant that the subscription, and everything else, closed when the using statement ended.

Pulling the proper parts out of a using statement and putting everything on the same ClientName produced the expected behavior when running the application.

Upvotes: 1

Related Questions