Skip to content

Clients pool

Andrey Bulygin edited this page May 21, 2013 · 1 revision

Home

Each RedisClient instance represents one connection to Redis. If you create this instance directly (not using Clients pool) then calling the IDisposable.Dispose will dispose socket and close connection to Redis. If you are worried about the ammount of redis connections your application makes you can use clients pool

[Test]
public void ClientsPool()
{
	using (var pool = RedisClient.CreateClientsPool())
	{
		IRedisClient cli1, cli2;
		using (cli1 = pool.CreateClientAsync(ConnectionString).Result)
		{
			cli1.SetAsync("Key", "Value").Wait();
		}
		using (cli2 = pool.CreateClientAsync(ConnectionString).Result)
		{
			cli2.GetAsync("Key").Wait();
		}
		Assert.AreEqual(cli1, cli2);
	}
}

Please consider the code above. cli1 and cli2 whould be the same instances, since IDisposable.Dispose won't close connection but only will return client to pool.

Use RedisClient.CreateClientsPool() factory method to create clients pool. This method has some overloads so you can pass inactivity timeout and pool size. Then inactivity timeout passes RedisClient is disconnected from Redis and disposed.

If RedisClient was disconnected, disposed, was called QUIT command or you've switched to Pub/Sub mode then this client can't be returned to pool. Such clients would be disposed if IDisposable.Dispose() is called.

Each instance of clients pool actually can manage many pools, one for each connection string.

If you dispose clients pool then all clients that are in pool will be disconnected from Redis and disposed.

Clone this wiki locally