c# – Redis serviceStack池连接客户端
发布时间:2020-12-15 04:31:21 所属栏目:百科 来源:网络整理
导读:我正在设计一个使用Redis作为数据库的Web服务,我想知道使用Redis连接到StackService客户端的最佳做法. 关键是我读过Redis,发现与服务器进行交互的最佳方法是使用一个并发连接. 问题是,尽管我每次使用PooledRedisClientManager时,Web客户端向Web服务发出请求,
我正在设计一个使用Redis作为数据库的Web服务,我想知道使用Redis连接到StackService客户端的最佳做法.
关键是我读过Redis,发现与服务器进行交互的最佳方法是使用一个并发连接. 问题是,尽管我每次使用PooledRedisClientManager时,Web客户端向Web服务发出请求,我还可以连接一个连接到redis服务器的客户端(打开的连接),并且这个连接的客户端数量增加而不会限制消耗更多的更多的记忆 示例“故障”代码: PooledRedisClientManager pooledClientManager = new PooledRedisClientManager("localhost"); var redisClient = pooledClientManager.GetClient(); using (redisClient) { redisClient.Set("key1","value1"); } 我做了什么来解决问题,是创建一个使用静态RedisClient var实现单例模式的类;如果redisClient未初始化,则会创建一个新的,如果是,则返回初始化的. 解: 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个客户端连接到服务器. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |