我正在使用@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>