【问题标题】:Unknown command error when using multithread to set redis使用多线程设置redis时出现未知命令错误
【发布时间】:2011-07-13 04:20:25
【问题描述】:

我正在使用ServiceStack.Redis C# 客户端与 Redis 对话。

请求很少,一切正常,但是当我让 LoadRunner 请求它或使用 multi-threading 发出请求时,我收到一些错误,说我正在使用错误的命令。

我检查了错误,似乎它切断了命令,或者它搞砸了。

这是我的代码,非常简单。有没有人遇到过这个问题?当我使用多线程调用 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
    }

【问题讨论】:

    标签: c# redis servicestack.redis


    【解决方案1】:

    您可能同时从多个线程使用相同的 Redis 连接。两个线程可能同时发送命令或等待回复。当这种情况发生时,一个线程接收到另一个线程的数据。这会导致您的错误。

    如果每个线程使用一个 Redis 客户端(而不是每个 ImpresstionQueueService 一个客户端),则每个线程可以同时发送命令而不会相互干扰。

    或者,您可以仅为单个请求(您在错误位置上方注释掉)创建一个客户端。这种替代方案的缺点是每次新连接的开销(可能很大或很小或不明显)。

    【讨论】:

    • 您应该为多线程应用程序使用PooledRedisClientManager(至少对于 v3 - v4 对于最常见的情况有一个新的客户端管理器),而不是直接使用客户端,如下所示:@987654323 @.
    猜你喜欢
    • 1970-01-01
    • 2016-08-18
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    相关资源
    最近更新 更多