MichaCo / CacheManager

CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
http://cachemanager.michaco.net
Apache License 2.0
2.34k stars 457 forks source link

How to use redis connection string in <connectionStrings> while setting enableKeyspaceNotifications="true" #254

Open theperiscope opened 5 years ago

theperiscope commented 5 years ago

Hi @MichaCo ,

Is there a way to configure CacheManager to use a redis connection string in web.config's <connectionStrings> section, while enabling enableKeyspaceNotifications? I know there is a way to use <connectionStrings> but then I can't find a way to set enableKeyspaceNotifications = true if I did it.

I was thinking it would be helpful if the <cacheManager>/<connections>/<connection> tag allowed for a connectionStringName attribute that points to a defined <connectionStrings> entry. As-is my redis connection strings are duplicated - one in the <connectionStrings> section for general redis operations, and one in the CacheManager... I would love to find a way to avoid it.

Thanks for the great work!

MichaCo commented 5 years ago

the keyspace notifications flag is specific to CacheManager and will not be recognized in the connection string parser. The <connectionStrings> section is a generic section and cannot be modified or anything either.

That being said, you could use CacheManager's configuration and use the static RedisConfigurations object to get a Redis configuration by name. The object returned has a ConnectionString property which can be passed in to a StackExchange.Redis.ConnectionMultiplexer if you want to use it outside of CacheManager.

theperiscope commented 5 years ago

Thanks for the suggestion, I think it may be useful for my scenario.

Actually, I had different suggestion above - to add new connectionStringName attribute to the <connection> tag in <cacheManager.Redis> section.

The reason for my suggestion is that the <connectionStrings> section is standard in .NET and as such is more frequently by existing projects/solutions. It would be nice if CacheManager can take advantage of them, while keeping its specific needs (e.g. enableKeyspaceNotifications) as part of the <cacheManager.Redis/connections/connection>. I hope this is clearer explanation, below is an example of my proposal.

  <connectionStrings>
    <add name="abc" connectionString="127.0.0.1:6379,ssl=False,allowAdmin=True,abortConnect=False,defaultDatabase=0,connectTimeout=500,connectRetry=3" />
  </connectionStrings>
  ...
  <cacheManager.Redis xmlns="http://cachemanager.michaco.net/schemas/RedisCfg.xsd">
    <connections>
      <connection id="redis1" enableKeyspaceNotifications="true" connectionStringName="abc" />
    </connections>
  </cacheManager.Redis>
MichaCo commented 5 years ago

Yeah, sounds like it would make kinda sense to reduce the redundancy of having the connection string twice. Although, does it really matter in production scenarios where this stuff is set in some dynamic/automated way anyways?

But considering that the old configuration system is pretty much dead in favor of the Extensions.Configuration stuff, I'm not planning to invest any time soon into adding things like that. If you want to give it a try though, feel free to send a PR ;)