简体   繁体   English

如何在运行时通过CacheManager修改加载的配置

[英]How modify loaded configuration by CacheManager in runtime

I'm using @Victor P solution to manage the cache in my application. 我正在使用@Victor P 解决方案来管理应用程序中的缓存。 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. 配置是从应用程序设置中加载的,但是我们的策略是不在代码中添加敏感信息,另一方面,Redis的生产实例需要进行身份验证。 This password is loaded from the environment variables, but I can't find the way to modify the Redis configuration in runtime. 此密码是从环境变量加载的,但是我找不到在运行时修改Redis配置的方法。

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. 我猜,从app / web.config中删除秘密始终是一个问题。 There is a documentation post which explains some options . 有一个文档发布,解释了一些选项

Regarding CacheManager. 关于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>部分而不是cacheManager.Redis部分来配置Redis,然后将该连接字符串存储在单独的“秘密”文件中。

<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 . 您可以“欺骗” cachemanager,并通过RedisConfigurations通过代码仅添加redis配置。 And reference the config key as usual. 并照常参考配置键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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