繁体   English   中英

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

[英]How modify loaded configuration by CacheManager in runtime

我正在使用@Victor P 解决方案来管理应用程序中的缓存。 配置是从应用程序设置中加载的,但是我们的策略是不在代码中添加敏感信息,另一方面,Redis的生产实例需要进行身份验证。 此密码是从环境变量加载的,但是我找不到在运行时修改Redis配置的方法。

这就是我们现在的做法

// 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();

配置示例:

<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>

我猜,从app / web.config中删除秘密始终是一个问题。 有一个文档发布,解释了一些选项

关于CacheManager。 您可以使用<connectionStrings>部分而不是cacheManager.Redis部分来配置Redis,然后将该连接字符串存储在单独的“秘密”文件中。

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

虽然我认为那还是很愚蠢的。 因此,最好的方法是完全通过代码配置该部分并从某个安全存储读取机密。 顺便说一句,环境变量也不是绝对安全的。

您可以“欺骗” cachemanager,并通过RedisConfigurations通过代码仅添加redis配置。 并照常参考配置键。

暂无
暂无

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

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