仅当找到/存在实例时,才使用Redis进行缓存
本文关键字:Redis 缓存 实例 存在 | 更新日期: 2023-09-27 18:29:51
我想使用Redis进行缓存,但如果在运行时找不到Redis实例,我仍然希望我的服务能够正常工作。
在实践中有这样的例子吗?
您可以在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());
}
...
}