Redis的serviceStack池连接客户端 [英] Redis serviceStack pooled connection client
问题描述
我设计它使用Redis的作为数据库的Web服务,我想知道使用Redis的与StackService客户端连接的最佳实践。
I'm designing a web service which uses Redis as a database, and I want to know the best practices for using Redis connecting with StackService client.
点是,我一直在阅读有关Redis的,我发现,与服务器交互的最佳方法是使用一个单一的并发连接。
The point is that I've been reading about Redis and I found that the best way to interact with the server is by using a single concurrent connection.
现在的问题是,尽管我M使用的 PooledRedisClientManager 每一个网络客户端向web服务,我得到一个更多的客户(开连接)Redis的服务器提出请求,这个数字连接的客户端增加无限制消耗更多的时间以及更多的内存
The problem is that despite I'm using PooledRedisClientManager each time that a web client makes a request to the web service I get a one more connected client (opened connection) to the redis server and this number of connected client increases without limit consuming more and more memory.
样品'故障'代码:
PooledRedisClientManager pooledClientManager = new PooledRedisClientManager("localhost");
var redisClient = pooledClientManager.GetClient();
using (redisClient)
{
redisClient.Set("key1", "value1");
}
我没有什么要解决的问题,就是创建实现Singleton模式的类使用静态 RedisClient
VAR;这意味着,如果在 redisClient
未初始化创建一个新的,如果是,则返回初始化之一。
What I did to solve the problem, is create a class implementing the singleton pattern with a static RedisClient
var; Which if the redisClient
is not initialized creates a new one, and if it is, returns the initialized one.
解决方案:
public class CustomRedisPooledClient
{
private static CustomRedisPooledClient _instance = null;
public RedisClient redisClient = null;
// Objeto sincronización para hacer el Lock
private static object syncLock = new object();
private CustomRedisPooledClient()
{
redisClient = new RedisClient("localhost");
}
public static CustomRedisPooledClient GetPooledClient()
{
if (_instance == null)
{
lock (syncLock)
{
if (_instance == null)
{
_instance = new CustomRedisPooledClient();
}
}
}
return _instance;
}
}
CustomRedisPooledClient customRedisPooledClient = CustomRedisPooledClient.GetPooledClient();
using (customRedisPooledClient.redisClient)
{
customRedisPooledClient.redisClient.Set("key1", "value1");
}
这是一个好的做法呢?
感谢你在前进!
推荐答案
我用 PooledRedisClientManager
和正常工作:
示例代码的我只运行一次
static PooledRedisClientManager pooledClientManager = new PooledRedisClientManager("localhost");
和代码,我在许多线程运行:
and code I run on many threads:
var redisClient = pooledClientManager.GetClient();
using (redisClient)
{
redisClient.Set("key" + i.ToString(), "value1");
}
和我只有11客户端连接到服务器。
and I have only 11 clients connected to the server.
这篇关于Redis的serviceStack池连接客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!