【发布时间】:2019-01-28 07:49:30
【问题描述】:
我正在开发一个微服务架构应用程序,我在其中使用 Redis 缓存来缓存常用信息。问题是连接的客户端数量不断增加,我不知道为什么。
我正在从 ASP.NET Web API 和 Web 作业访问 Redis 缓存。用于连接的 NuGet 包是“StackExchange.Redis”(https://github.com/StackExchange/StackExchange.Redis)。
我在代码中连接Redis的方式如下:
connection = ConnectionMultiplexer.Connect(configurationOptions);
connection.ConnectionFailed += ConnectionFailed;
connection.ConnectionRestored += ConnectionRestored;
connection.InternalError += InternalError;
if (!connection.IsConnected)
{
//_traceSource.TraceWarning($"Connection to REDIS '{_endpointAddress}' was not established.");
}
database = connection.GetDatabase();
return database;
此外,我还实现了 Dispose() 方法以确保正确断开连接:
public void Dispose()
{
connection?.Close(true);
}
【问题讨论】:
-
看起来您的应用创建了新连接,但没有关闭它们
-
你应该共享同一个静态
ConnectionMultiplexer
标签: c# redis stackexchange.redis