【问题标题】:How modify loaded configuration by CacheManager in runtime如何在运行时通过 CacheManager 修改加载的配置
【发布时间】:2023-03-18 18:45:01
【问题描述】:

我正在使用@Victor P solution 来管理我的应用程序中的缓存。 配置是从应用设置中加载的,但是我们有在代码中不添加敏感信息的策略,另一方面,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>

【问题讨论】:

    标签: c# redis ninject cachemanager


    【解决方案1】:

    我想,从 app/web.config 中删除机密本身就是一个问题。 有一个documentation post which explains some options

    关于缓存管理器。您可以使用 &lt;connectionStrings&gt; 部分来配置 Redis,而不是 cacheManager.Redis 部分,然后将该连接字符串存储在单独的“秘密”文件中

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

    在我看来,这仍然很愚蠢。因此,最好的方法是完全通过代码配置该部分并从某个安全存储中读取机密。顺便说一句,环境变量也不安全。

    您可以通过RedisConfigurations 通过代码“欺骗”缓存管理器并仅添加 redis 配置。并像往常一样引用配置键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-11
      • 2011-08-08
      • 2020-06-12
      • 2018-12-19
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 2014-05-15
      相关资源
      最近更新 更多