1
我正在使用C#客戶端ServiceStack.Redis與Redis進行通信。使用多線程設置redis時發生未知命令錯誤
除了少數要求一切正常,但是當我得到LoadRunner的要求或使用多線程提出要求,我得到一些錯誤說我使用了錯誤的命令。
我檢查錯誤,看起來它切斷了命令,或者它搞砸了。
這是我的代碼,很簡單。有沒有人遇到過這個問題?我使用多線程調用Push方法時發生錯誤。
public class ImpresstionQueueService : IQueueService<InsertImpressionRequest>
{
private string _queueName;
private string _host;
private static IRedisClient redisClient = new RedisClient(ConfigHost);
private static string ConfigHost
{
get
{
return ConfigurationManager.AppSettings.Get("redis_host");
}
}
private string Host
{
get
{
if (!string.IsNullOrEmpty(_host))
return _host;
else
{
return ConfigurationManager.AppSettings.Get("redis_host");
}
}
}
public ImpresstionQueueService(string queue_name)
{
this._queueName = queue_name;
}
public ImpresstionQueueService(string host, string queu_name)
{
this._queueName = queu_name;
this._host = host;
}
#region IQueueService<InsertImpressionRequest> Members
class testData
{
}
public int Push(InsertImpressionRequest value)
{
try
{
//using (var redisClient = new RedisClient(this.Host))
{
//ser
string ser_value = TypeSerializer.SerializeToString<InsertImpressionRequest>(value);
//push
redisClient.AddItemToList(this._queueName, ser_value);//here will be error
}
}
catch (Exception ex)
{
HLogger.GetLogger("RedisLogger").Error(ex.Message + ex.StackTrace);
}
//throw new NotImplementedException();
return 1;
}
public InsertImpressionRequest Pop()
{
InsertImpressionRequest request = null;
//using (var redisClient = new RedisClient(this.Host))
{
string pop_string_value = redisClient.PopItemFromList(this._queueName);
//deseri
if (pop_string_value != null)
{
request = TypeSerializer.DeserializeFromString<InsertImpressionRequest>(pop_string_value);
}
}
return request;
}
#endregion
}
您應該爲多線程應用程序使用'PooledRedisClientManager'(至少v3版本 - v4在最常見的情況下有一個新的客戶端管理器),而不是直接使用客戶端,如下所示:'using(var redis = pooledRedisClientManager .GetClient())redis.Get(「blah」)'。 – Raphael 2015-05-08 18:29:10