I am using servicestack for my redisclient in my class. I have one redis client as the following:
public class MySuperClass{
....
RedisClient client = new RedisClient("localhost", 6379);
public int MySuperProperty{get; set:}
....
}
And the following is how I use it to make sure that it is properly disposed after I use it:
private void GetInfoFromRedis(object sender, EventArgs e) {
using (client) {
client.Set<Human>("RedisKey", new Human {
Age = 29,
Height = 170,
Name = "HumanName"
});
}
}
My question is after I dispose client
if I make another request to redis using the same disposed client
, disposed client makes another connection to redis database successfully but this time the connection stays in the CLIENT LIST.
Please see the ServiceStack.Redis documentation for the correct usage of using ServiceStack Redis Client, i.e. you should be using a Redis ClientManager as a singleton, preferably one that you register in an IOC, e.g:
container.Register<IRedisClientsManager>(c =>
new RedisManagerPool("localhost:6379"));
Then have that injected in your classes, e.g:
public class MySuperClass
{
public IRedisClientsManager RedisManager { get; set; }
}
Then you can resolve a client from the Redis ClientManger within a using, e.g:
private void GetInfoFromRedis(object sender, EventArgs e)
{
using (var client = RedisManager.GetClient())
{
client.Set<Human>("RedisKey", new Human {
Age = 29,
Height = 170,
Name = "HumanName"
});
}
}
}
If you don't use an IOC you can populate IRedisClientsManager in a static property, e.g:
public class MySuperClass
{
public static IRedisClientsManager RedisManager =
new RedisManagerPool("localhost:6379");
}
But the important thing is to resolve a redis client from a IRedisClientsManager
and then dispose of it after immediately use, e.g. within a using statement:
using (var redis = RedisManager.GetClient()) { ... }