简体   繁体   中英

How modify loaded configuration by CacheManager in runtime

I'm using @Victor P solution to manage the cache in my application. The configuration is loaded from the application settings, but we have a policy of not add sensitive information in the code, and on the other hand, the production instance of Redis requires authentication. This password is loaded from the environment variables, but I can't find the way to modify the Redis configuration in runtime.

Here is how we are doing it now

// Locad configuration of cache type: MemoryCache or RedisCache
string cacheManagerName = ConfigurationManager.AppSettings["CacheManagerName"];

// Build cache configuration from configuration section
var config = ConfigurationBuilder.LoadConfiguration(cacheManagerName);

//TODO: Modify config if the variable environment for the password is set
// This will only necessary if the cache type is Redis

//Create cachemanager instance
_kernel.Bind(typeof(ICacheManager<>)).ToMethod((ctx) => CacheFactory.FromConfiguration(ctx.GenericArguments[0], config)).InSingletonScope();

Configuration example:

<add key="CacheManagerName" value="RedisCache" />

<cacheManager xmlns="http://cachemanager.michaco.net/schemas/CacheManagerCfg.xsd">
    <managers>
      <cache name="MemoryCache" updateMode="None" enableStatistics="false" enablePerformanceCounters="true">
        <handle name="default" ref="MemoryCacheHandle" />
      </cache>
      <cache name="RedisCache" updateMode="Up" enablePerformanceCounters="true"
             enableStatistics="false" backplaneName="RedisConfigurationId"
             backplaneType="CacheManager.Redis.RedisCacheBackplane, CacheManager.StackExchange.Redis"
             serializerType="CacheManager.Serialization.Json.JsonCacheSerializer, CacheManager.Serialization.Json">
        <handle name="RedisConfigurationId" ref="RedisCacheHandle" isBackplaneSource="true"/>
      </cache>
    </managers>
    <cacheHandles>
      <handleDef id="MemoryCacheHandle" type="CacheManager.SystemRuntimeCaching.MemoryCacheHandle`1, CacheManager.SystemRuntimeCaching"
        defaultExpirationMode="Sliding" defaultTimeout="30m" />
      <handleDef  id="RedisCacheHandle" type="CacheManager.Redis.RedisCacheHandle`1, CacheManager.StackExchange.Redis"
        defaultExpirationMode="Sliding" defaultTimeout="30m" />
    </cacheHandles>
  </cacheManager>
  <cacheManager.Redis xmlns="http://cachemanager.michaco.net/schemas/RedisCfg.xsd">
    <connections>
      <connection id="RedisConfigurationId"
                       allowAdmin="true"
                       password=""
                       ssl="false"
                       sslHost="">
        <endpoints>
          <endpoint host="127.0.0.1" port="6379" />
        </endpoints>
      </connection>
    </connections>
  </cacheManager.Redis>

Removing secrets from an app/web.config was always an issue by itself I guess. There is a documentation post which explains some options .

Regarding CacheManager. You can use the <connectionStrings> section for configuring Redis, instead of the cacheManager.Redis section, and then store that connection string in a separated "secret" file

<connectionStrings configSource="ConnectionStrings.config"> </connectionStrings>

That's still pretty stupid though in my opinion. So the best way is to configure that part entirely by code and read secrets from some secure store. Btw, environment variables are not secure at all either.

You can "trick" cachemanager and add just the redis configuration by code via RedisConfigurations . And reference the config key as usual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM