1

我正在尝试使用 ServiceStack.Redis 访问 .Net 应用程序中的键空间通知。我是 Redis 的新手。

我通过命令在缓存上启用了事件通知:

CONFIG SET notify-keyspace-events KEs

我正在订阅.Net 中的频道“ key* :*”。以下是我的代码:

const string ChannelName = "__key*__:*";
    using (var redisConsumer = new RedisClient("localhost:6379"))
    using (var subscription = redisConsumer.CreateSubscription())
    {
        subscription.OnSubscribe = channel =>
        {
            Console.WriteLine(String.Format("Subscribed to '{0}'", channel));
        };
        subscription.OnUnSubscribe = channel =>
        {
            Console.WriteLine(String.Format("UnSubscribed from '{0}'", channel));
        };
        subscription.OnMessage = (channel, msg) =>
        {
            Console.WriteLine(String.Format("Received '{0}' from channel '{1}'", 
                msg, channel));
        };             

        Console.WriteLine(String.Format("Started Listening On '{0}'", ChannelName));
        subscription.SubscribeToChannels(ChannelName); //blocking
   }

从另一个 .Net 应用程序中,我将新数据添加到缓存中。我期待收到一个事件(在 OnMessage 中)。在缓存中添加新项目时,应用程序未捕获任何事件。

但是,当我在 redis-cli.exe 上运行命令“psubscribe ' key* :*'”时,它会正确捕获事件。(当我将新项目添加到缓存时,它会在控制台窗口中显示事件详细信息。)

我无法在我的应用程序中捕获相同的内容。我在这里错过了什么吗?

4

1 回答 1

1

利用subscription.SubscribeToChannelsMatching(ChannelName);

于 2014-11-27T09:54:26.750 回答