打开与Redis的连接有时需要很长时间.看起来它取决于连接线程的数量,也许还有PC配置.我在两个带有4核CPU的工作站上运行50个线程的测试,打开连接需要70-100ms,在8核工作站和8核登台服务器上需要1000-1500ms,有时甚至更多.奇怪的依赖,但它'可再现.当IIS应用程序池重新启动,并且所有线程都尝试重新连接时,会导致缓存停机等.为了获得合理的连接时间,我需要改变什么?
我使用BookSleeve客户端,这里是代码示例:
static void Main(string[] args)
{
for (var i = 0; i < threadCount; i++)
threads.Add(new Thread(RunThread));
foreach (var thread in threads)
thread.Start();
foreach (var thread in threads)
thread.Join();
}
static void RunThread()
{
var connection = TryGetConnection();
while (connection == null)
{
connection = TryGetConnection();
}
}
static RedisConnection TryGetConnection()
{
var connection = currentConnection;
if ((connection != null) && (connection.State == RedisConnectionBase.ConnectionState.Open))
return connection;
lock (syncRoot)
{
if ((currentConnection != null) && (currentConnection.State == RedisConnectionBase.ConnectionState.Open))
return currentConnection; …Run Code Online (Sandbox Code Playgroud)