Reputation: 61773
I'm reviewing some old code. We have cache keys which hold small amounts of binary data.
Every time we successfully retrieve one of these cached items, we call:
RedisController.GetConnection().GetSubscriber().SubscribeAsync(cacheKey, onMessageReceived);
Where the channel name is the name of the cache key.
We call this method every time the cache key is gotten which can be dozens of times in quick succession.
My questions is does subscribing to an already existing channel do any work, or is the request ignored cheaply? There doesn't appear to be any methods to do something along the lines of:
var subscriber = RedisController.GetConnection().GetSubscriber();
if(!subscriber.isSubscribed(cacheKey) {
RedisController.GetConnection().GetSubscriber().SubscribeAsync(cacheKey, onMessageReceived);
}
Upvotes: 1
Views: 587
Reputation: 6484
In SE.Redis, subscribing to an existing Redis channel re-issues the SUBSCRIBE
command to the underlying Redis server topology and adds the message handler to the internal list of handlers, managed by the library itself; on the Redis side, however, subscribing to an existing channel is simply ignored.
As to how to retrieve the number of existing subscriptions to a given channel, SE.Redis exposes the GetSubscriberCounts() method to do that, which is unfortunately not public.
If you wish to keep track of your current channels / subscriptions you may use an external, different structure - such as a Dictionary<string>
of cache keys or alike.
Upvotes: 1