2

我想使用 Redis 进行缓存,但如果在运行时找不到 Redis 实例,我仍然希望我的服务能够正常工作。

在实践中有没有这样的例子?

4

1 回答 1

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());
    }
    ...
}
于 2013-09-04T09:57:06.067 回答