我想使用Redis进行缓存,但如果在运行时找不到Redis实例,我仍然希望我的服务能够正常运行。
在实践中是否有这方面的例子?
答案 0 :(得分:1)
您可以在AppHost配置方法中执行以下操作
public override void Configure(Container container)
{
...
try
{
var redisManager = new PooledRedisClientManager("localhost:6379");
// do some sort of test to see if we can talk to the redis server
var client = redisManager.GetCacheClient();
var fakeKey = "_________test_______";
client.Add(fakeKey, 1);
client.Remove(fakeKey);
// if it worked register the cacheClient
container.Register(c => redisManager.GetCacheClient()).ReusedWithin(ReuseScope.None);
}
catch (Exception ex)
{
// fall back to in memory cache
// Log some sort of warning here.
container.Register<ICacheClient>(c => new MemoryCacheClient());
}
...
}