redis偶发性连接不上 返回
请问,怎么设置当redis连接不上或者异常的时候直接取数据库?
异常如下:
System.Exception: 【redis-da16bd3-xxxxx-01.dcs.huaweicloud.com:6379/0】状态不可用,等待后台检查程序恢复方可使用。Connect to server timeout
---> CSRedis.Internal.IO.RedisSocketException: Connect to server timeout
at CSRedis.Internal.IO.RedisSocket.Connect(EndPoint endpoint, Int32 timeout)
at CSRedis.Internal.RedisConnector.Connect(Int32 timeout)
at CSRedis.RedisClient.Connect(Int32 timeout)
at CSRedis.RedisClientPool.Return(Object`1 obj, Exception exception, Boolean isRecreate)
--- End of inner exception stack trace ---
at CSRedis.Internal.ObjectPool.ObjectPool`1.getFree(Boolean checkAvailable)
at CSRedis.Internal.ObjectPool.ObjectPool`1.Get(Nullable`1 timeout)
at CSRedis.CSRedisClient.GetAndExecute[T](RedisClientPool pool, Func`2 handler, Int32 jump, Int32 errtimes)
at CSRedis.CSRedisClient.ExecuteScalar[T](String key, Func`3 hander)
at CSRedis.CSRedisClient.Exists(String key)
热忱回答(4)
-
fate sta VIP0
2021/10/18把你重写的二级缓存类,下面2个方法处理一下
public bool ContainsKey<V>(string key)
{
连不上失败返回false
}
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
{
连不上不抛出异常
}
0 回复 -
liftlei VIP0
2021/10/18@fate sta:我用的csredis 这样写有问题吗?
public class CsRedisCache : ICacheService
{
private readonly CSRedisClient _service;
public CsRedisCache(CSRedisClient redisClient)
{
_service = redisClient;
}
public void Add<V>(string key, V value)
{
_service.Set(key, value);
}
public void Add<V>(string key, V value, int cacheDurationInSeconds)
{
_service.Set(key, value, cacheDurationInSeconds);
}
public bool ContainsKey<V>(string key)
{
return _service.Exists(key);
}
public V Get<V>(string key)
{
return _service.Get<V>(key);
}
public IEnumerable<string> GetAllKey<V>()
{
return _service.Keys("*");
}
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
{
if (ContainsKey<V>(cacheKey))
{
return Get<V>(cacheKey);
}
else
{
var result = create();
Add(cacheKey, result, cacheDurationInSeconds);
return result;
}
}
public void Remove<V>(string key)
{
_service.Del(key);
}
}
0 回复 -
fate sta VIP0
2021/10/18你要要加try处理,不然报错了程序就报错了
0 回复 -
liftlei VIP0
2021/10/18@fate sta:好的,我试试,谢谢!
0 回复