servicestack,C#,Redis,servicestack" /> servicestack,C#,Redis,servicestack" />

C# Redis serviceStack池连接客户端

C# Redis serviceStack池连接客户端,c#,redis,servicestack,C#,Redis,servicestack,我正在设计一个使用Redis作为数据库的web服务,我想知道使用Redis连接StackService客户端的最佳实践 关键是我一直在阅读有关Redis的文章,我发现与服务器交互的最佳方式是使用单个并发连接 问题是,尽管我使用的是PooledRedisClientManager,但每次web客户端向web服务发出请求时,我都会得到一个连接到redis服务器的多个客户端(打开的连接),并且连接的客户端数目的增加不会限制消耗越来越多的内存 示例“故障”代码: PooledRedisClientMan

我正在设计一个使用Redis作为数据库的web服务,我想知道使用Redis连接StackService客户端的最佳实践

关键是我一直在阅读有关Redis的文章,我发现与服务器交互的最佳方式是使用单个并发连接

问题是,尽管我使用的是PooledRedisClientManager,但每次web客户端向web服务发出请求时,我都会得到一个连接到redis服务器的多个客户端(打开的连接),并且连接的客户端数目的增加不会限制消耗越来越多的内存

示例“故障”代码:

PooledRedisClientManager pooledClientManager = new PooledRedisClientManager("localhost");
var redisClient = pooledClientManager.GetClient();
using (redisClient)
{
   redisClient.Set("key1", "value1");
}
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");
}
为了解决这个问题,我所做的是创建一个类,用一个静态的
RedisClient
var实现singleton模式;如果未初始化
redisClient
,则会创建一个新的客户端,如果已初始化,则返回已初始化的客户端

解决方案:

PooledRedisClientManager pooledClientManager = new PooledRedisClientManager("localhost");
var redisClient = pooledClientManager.GetClient();
using (redisClient)
{
   redisClient.Set("key1", "value1");
}
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");
我在许多线程上运行的代码:

var redisClient = pooledClientManager.GetClient();
using (redisClient)
{
    redisClient.Set("key" + i.ToString(), "value1");
}

而且我只有11个客户端连接到服务器。

如果我这样做,我会在浏览器发出的每个请求中获得一个新线程。我已经对它进行了调试,当行redisClient.Set(“key”+I.ToString(),“value1”)时创建了一个新线程(客户机);被执行,我失去了控制,似乎它将永远开放。我做了一个测试,刷新了调用服务URL的网页,我已经连接了100个客户端。可能问题是我在每个请求上都运行了代码,是吗?你确定你没有每次都运行“PooledRedisClientManager pooledClientManager=new PooledRedisClientManager(“localhost”);”吗?是的,我在每个请求中都运行它,这是错误的吗?正确的用法是什么?您需要定义一个保存pooledClientManager的静态成员。对于每个请求,您都需要引用该静态成员(不创建它的新实例),并运行“using(var redisClient=…”代码。只需确保您不会多次实例化PooledRedisClientManager。为什么您从池中提取redisClient但不使用它?而是使用pooledClientManager?编写问题是一个错误,现在它已更正,但我会编辑您的问题,因为您的“错误代码”现在起作用,并且提供了解决方案不理想。添加问题所在,并参考公认的答案以获得理想的解决方案。