Reputation: 15242
I am using an ServiceStack IRedis client as follows
public static IRedisList<MyType> getList(string listkey)
{
using(var redis = new RedisClient())
{
var client = redis.As<MyType>();
return client.Lists[listkey];
}
}
public void AddSomething(MyType newType)
{
var list = getList("somekey);
list.Add(newType);.
}
according to the Redis-Server output, everytime I do this a new client connection is added, and it never is getting disposed. (Client count is always increasing).
Should I not be manipulating the IRedisList
as such?
Upvotes: 1
Views: 645
Reputation: 143339
IRedisList
is like a proxy wrapper for a redis server-side list which includes a reference to the RedisClient it was created with in order to talk Redis.
The using(var redis = new RedisClient()) { ... }
statement does dispose of the Client connection, unfortunately when you make any further calls with the Disposed client it re-opens the connection again and since it isn't in a using statement or not manually disposed will keep the connection open.
To prevent this from happening, you should not be accessing the RedisList proxy outside of the using scope of the RedisClient in which it was created. If you still want to reference the list outside of this scope you should clone the contents into an In-Memory list with:
var disconnectedInMemoryCopy = client.Lists[listKey].ToList();
This returns a populated normal C# List<T>
that's de-coupled from the RedisList proxy.
Upvotes: 2