Caching 为什么到Azure Redis缓存的连接如此之高?
我使用Azure Redis缓存的场景是单台机器查询缓存的高负载。这台机器大约每秒获取和设置20个项目。在白天这会增加,在夜间这会减少 到目前为止,一切进展顺利。今天我意识到“连接的客户端”的标准非常高,尽管我只有一个客户端不断地获取和设置项目。这是指标的屏幕截图,我的意思是: 我的代码如下所示:Caching 为什么到Azure Redis缓存的连接如此之高?,caching,azure,redis-cache,azure-redis-cache,Caching,Azure,Redis Cache,Azure Redis Cache,我使用Azure Redis缓存的场景是单台机器查询缓存的高负载。这台机器大约每秒获取和设置20个项目。在白天这会增加,在夜间这会减少 到目前为止,一切进展顺利。今天我意识到“连接的客户端”的标准非常高,尽管我只有一个客户端不断地获取和设置项目。这是指标的屏幕截图,我的意思是: 我的代码如下所示: public class RedisCache<TValue> : ICache<TValue> { private IDatabase cache; pri
public class RedisCache<TValue> : ICache<TValue>
{
private IDatabase cache;
private ConnectionMultiplexer connectionMultiplexer;
public RedisCache()
{
ConfigurationOptions config = new ConfigurationOptions();
config.EndPoints.Add(GlobalConfig.Instance.GetConfig("RedisCacheUrl"));
config.Password = GlobalConfig.Instance.GetConfig("RedisCachePassword");
config.ConnectRetry = int.MaxValue; // retry connection if broken
config.KeepAlive = 60; // keep connection alive (ping every minute)
config.Ssl = true;
config.SyncTimeout = 8000; // 8 seconds timeout for each get/set/remove operation
config.ConnectTimeout = 20000; // 20 seconds to connect to the cache
connectionMultiplexer = ConnectionMultiplexer.Connect(config);
cache = connectionMultiplexer.GetDatabase();
}
public virtual bool Add(string key, TValue item)
{
return cache.StringSet(key, RawSerializationHelper.Serialize(item));
}
公共类RedisCache:ICache
{
私有IDatabase缓存;
专用连接多路复用器连接多路复用器;
公共缓存()
{
ConfigurationOptions config=新的ConfigurationOptions();
config.EndPoints.Add(GlobalConfig.Instance.GetConfig(“RedisCacheUrl”);
config.Password=GlobalConfig.Instance.GetConfig(“RedisCachePassword”);
config.ConnectRetry=int.MaxValue;//如果连接中断,请重试连接
config.KeepAlive=60;//保持连接活动(每分钟ping一次)
config.Ssl=true;
config.SyncTimeout=8000;//每个get/set/remove操作超时8秒
config.ConnectTimeout=20000;//连接到缓存需要20秒
connectionMultiplexer=connectionMultiplexer.Connect(配置);
cache=connectionMultiplexer.GetDatabase();
}
公共虚拟bool Add(字符串键、TValue项)
{
返回cache.StringSet(key,RawSerializationHelper.Serialize(item));
}
我没有创建这个类的多个实例,所以这不是问题所在。也许我不理解连接度量,它们真正的意思是我访问缓存的次数,但是,我认为这没有什么意义。有什么想法,或者有类似问题的人吗?StackExchange.Redis有竞争条件在某些情况下,这可能导致连接泄漏。这已在build 1.0.333或更新版本中修复 如果要确认这是您遇到的问题,请获取客户端应用程序的崩溃转储,并在调试器中查看堆上的对象。查找大量StackExchange.Redis.ServerEndPoint对象 此外,一些用户的代码中存在导致连接对象泄漏的错误。这通常是因为他们的代码在看到故障或断开连接状态时试图重新创建ConnectionMultiplexer对象。实际上不需要重新创建ConnectionMultiplexer,因为它在内部具有重新创建连接的逻辑这是必要的。只需确保在连接字符串中将abortConnect设置为false 如果确实决定重新创建连接对象,请确保在释放对该对象的所有引用之前先处置旧对象 以下是我们推荐的模式:
private static Lazy lazyConnection = new Lazy(() => {
return ConnectionMultiplexer.Connect("contoso5.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
});
public static ConnectionMultiplexer Connection {
get {
return lazyConnection.Value;
}
}
谢谢JonCole,这就是问题所在。1.0.333的更新修复了问题。问题似乎又回到了2.2.4版本。我可以确认。它又回到了2.2.4版本。你知道修复可能是什么吗?