CacheManager
CacheManager copied to clipboard
Enabling keyspace notifications causes keys in Redis to be encoded
I use Redis Desktop Manager (RDM) as a nice GUI client to our Redis instances. Up to this point, we've been using CacheManager with only a Redis handle (no in-memory) and I have been able to browse the keys in RDM just fine (even rendering the key namespaces as expected as virtual "folders").
Now I'm starting an effort to use Redis as the backplane to synchronize multiple processes using the cache. That works as expected (of course), but I've noticed that when I enable keyspace notifications like this:
configBuilder.WithRedisConfiguration(name, redisConnectionString, enableKeyspaceNotifications: true)
Then the keys in RDM look like they're being encoded:
The ability to browse the keys in RDM is extremely useful to us during development and troubleshooting - is there a way to prevent this encoding of the keys?
Looking at the source code for RedisCacheHandle, it looks like this behavior has to do with parsing the region from the key. But if I'm not using regions, can there be some way to tell the Redis handle that so it does not encode the keys?
Problem is that at that point the code doesn't know if you use regions or not ;) I'm only getting the key from the event. To build a key:region pair, the current implementation just uses a separator. If you'd use the same separator in your key (by coincidence) The logic would be messed up. That's why I decided to use that mechanism to make sure I can identify key and region properly.
An alternative would be
- Define region separator via configuration and maybe even throw an error if you try to use that char within a key
- Use first few bytes to indicate the length of the key. That would still end up in a partially unreadable key though
I know it is not ideal right now. But using this feature of keyspace notifications is a edge case scenario anyways and not that many are using it I think (guessing)...
I think the first option would be a good addition though, I might add that eventually.
I understand. Is there any workaround at all to avoid this while still using keyspace notifications? I rely heavily on the keys being human-readable for troubleshooting.
Don't use ":" in your keys to prevent encoding
Just reviewed the code, following is the line which leads to encoding the key potion
if (_redisConfiguration.KeyspaceNotificationsEnabled && key.Contains(":"))
CacheManager is agnostic about Redis namespaces in the keys, right? In other words, using : as the namespace separator in my keys is just a common convention; I can simply choose to use some other character, such as |, and CacheManager won't care. Correct?
yup