Search code examples
c#asp.netcachemanager

CacheManger Using Redis Multiplexer with Web.Config Configuration


I need to implement Michael solution using two Cache Instances like he explain in WhatIfRedisStopsWorkingHowDoIkeepMyAppRunning but using configuration in web.config.

Finally i only have this line of code

var defaultConfig = ConfigurationBuilder.LoadConfiguration("defaultCache");

I don`t find how to access to the ConnectionMultiplexer to hook me in the events or do it by config...

Is posible?


Solution

  • There are two ways to configure Redis via app/web.config in CacheManager, via ConnectionString

    <connectionStrings>
        <add name="redisFromConnectionStrings" connectionString="127.0.0.1:6379,allowAdmin=True,connectTimeout=11,ssl=False,abortConnect=False,connectRetry=10" />
    </connectionStrings>
    

    or Redis configuration section

    <cacheManager.Redis xmlns="http://cachemanager.michaco.net/schemas/RedisCfg.xsd">
    <connections>
      <connection id="redisAppConfig" allowAdmin="true" password="" ssl="false" sslHost="" connectionTimeout="11" database="3">
        <endpoints>
          <endpoint host="127.0.0.1" port="6379" />
        </endpoints>
      </connection>
    </connections>
    </cacheManager.Redis>
    

    :UPDATE: There is currently no option to access the connection multiplexer used by CacheManager. But you can pass in an existing multiplexer to the configuration.

    var defaultConfig = ConfigurationBuilder.LoadConfiguration("defaultCache");
    var multiplexer = ConnectionMultiplexer.Connect(...);
    
    defaultConfig = defaultConfig
                .Builder
                .WithRedisConfiguration("redisConfig", multiplexer )
                .Build();
    

    Of course you have to instantiate the multiplexer yourself and cannot use the web/app config anymore to configure the Redis part. You'd have to handle that yourself...