Redis를 데이터베이스로 사용하는 웹 서비스를 디자인하고 있으며 StackSvice 클라이언트와 연결하는 Redis 사용에 대한 유용한 정보를 알고 싶습니다.Redis serviceStack 풀링 된 연결 클라이언트
요점은 내가 Redis에 대해 읽었으며 서버와 상호 작용하는 가장 좋은 방법은 단일 동시 연결을 사용한다는 것입니다.
PooledRedisClientManager 웹 클라이언트가 웹 서비스에 요청할 때마다 웹 서버에 연결된 클라이언트 (연결이 열린 상태)가 하나 더 연결되어 있고 연결된 클라이언트의 수가 많습니다. 제한없이 증가하여 더 많은 메모리를 소비합니다.
샘플 '오류'코드 : 나는 문제를 해결하기 위해 무슨 짓을
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");
}
이 좋은 방법인가?
미리 감사드립니다.
** 풀에서 ** redisClient **를 가져 오지 만 사용하지 않는 이유는 무엇입니까? ** pooledClientManager **를 대신 사용하고 있습니까? – mythz
질문을 작성하는 실수였습니다. 이제 –
k, tho 귀하의 '오류 코드'가 작동하고 ** 해결 방법 **이 이상적이지 않기 때문에 귀하의 질문을 편집하게되었습니다. 문제가 무엇인지 추가하고 이상적인 솔루션에 대한 대답을 참조하십시오. – mythz