ServiceStack Redis erros: "Unexpected reply: *", "Protocol error: expected '$', got 'C'", lost connection and etc

ServiceStack Redis erros: "Unexpected reply: *", "Protocol error: expected '$', got 'C'", lost connection and etc

我做萝卜的时候是不是经常出错。

这是我的配置:

container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("localhost:6379"));
container.Register(c => c.Resolve<IRedisClientsManager>().GetCacheClient());

container.Register(new GameDispatchService(container.Resolve<IRedisClientsManager>()));

是class改变了radis中的数据

public class GameDispatchService {
        private readonly IRedisClientsManager redisManager;
        private Timer mTimer;

        public GameDispatchService(IRedisClientsManager redisManager) {
            this.redisManager = redisManager;

            mTimer = new Timer();

            mTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            mTimer.Interval = 1000;
            mTimer.Enabled = true;
        }

        private void OnTimedEvent(object sender, ElapsedEventArgs e) {
            mTimer.Stop();

            try {
                using (IRedisClient cacheClient = redisManager.GetClient()) {
                    var sessionPattern = "game:Created*";
                    var tSessionKeys = cacheClient.GetKeysStartingWith(sessionPattern).ToList();

                    if (!tSessionKeys.IsNullOrEmpty()) {
                        var list = cacheClient.GetAll<GameCachModel>(tSessionKeys);

                        foreach (var item in list) {
                            var lastKey = item.Key;
                            var newKey = $"game:{GameStatuses.Waiting}:{item.Value.GameNumber}";
                            item.Value.Status = GameStatuses.Waiting;

                            cacheClient.Remove(lastKey);
                            cacheClient.Add(newKey, item.Value);
                        }
                    }
                }
            } catch (Exception ex) {
                 var t = ex.Message;
                t.PrintDump();
            } finally {
                mTimer.Start();
            }

        }

        .......
    }

所以我从 redis

接收数据
    List<GameRetModel> gamesList = new List<GameRetModel>();

                try {
                    using (var cacheClient = this.GetCacheClient()) {
                        var sessionPattern = "game:Waiting*";
                        var tSessionKeys = cacheClient.GetKeysStartingWith(sessionPattern)?.ToList();

                        if (!tSessionKeys.IsNullOrEmpty()) {
                            var list = cacheClient.GetAll<GameCachModel>(tSessionKeys);

                            foreach (var item in list.Values) {
                                if (item.Rate >= request.MinVal && item.Rate <= request.MaxVal) {
                                    gamesList.Add(new GameRetModel {
                                        Author = item.Author,
                                        Avatar = item.Avatar,
                                        GameNumber = item.GameNumber,
                                        Count = item.Users.Count,
                                        Rate = item.Rate
                                    });
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.Message.PrintDump();
                    return new { error = true };
                }

                return new { error = false, gamesList = gamesList };
}

而且报了很多错,上网查了下,明白是多线程的问题,但是解决不了。

如果您知道如何正确操作,请提供帮助。

ServiceStack 版本 4.5.5 Redis 3.0.501

谢谢!

我收到以下错误:

1 .Message: 协议错误:应为“$”,得到 'C'

跟踪:

в ServiceStack.Redis.RedisNativeClient.ReadDeeplyNestedMultiDataItem()
   в ServiceStack.Redis.RedisNativeClient.ReadDeeplyNestedMultiData()
   в ServiceStack.Redis.RedisNativeClient.SendReceive[T](Byte[][] cmdWithBinaryArgs, Func`1 fn, Action`1 completePipelineFn, Boolean sendWithoutRead)
   в ServiceStack.Redis.RedisNativeClient.SendExpectDeeplyNestedMultiData(Byte[][] cmdWithBinaryArgs)
   в ServiceStack.Redis.RedisNativeClient.SendExpectScanResult(Byte[] cmd, Byte[][] args)
   в ServiceStack.Redis.RedisNativeClient.Scan(UInt64 cursor, Int32 count, String match)
   в ServiceStack.Redis.RedisClient.<ScanAllKeys>d__159.MoveNext()
   в System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   в System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   в loto.ServiceInterface.LotoServices.Post(GetGames request) в D:\Documents\Visual Studio 2015\Projects\loto\loto\loto.ServiceInterface\LotoServices.cs:строка 144
  1. 消息:意外回复:*2

跟踪:

в ServiceStack.Redis.RedisNativeClient.ParseSingleLine(String r)
   в ServiceStack.Redis.RedisNativeClient.ReadData()
   в ServiceStack.Redis.RedisNativeClient.SendReceive[T](Byte[][] cmdWithBinaryArgs, Func`1 fn, Action`1 completePipelineFn, Boolean sendWithoutRead)
   в ServiceStack.Redis.RedisNativeClient.SendExpectData(Byte[][] cmdWithBinaryArgs)
   в ServiceStack.Redis.RedisNativeClient.Set(String key, Byte[] value, Boolean exists, Int32 expirySeconds, Int64 expiryMs)
   в ServiceStack.Redis.RedisClient.<>c__DisplayClass21_0`1.<Add>b__0(RedisClient r)
   в ServiceStack.Redis.RedisClient.Exec[T](Func`2 action)
   в ServiceStack.Redis.RedisClient.Add[T](String key, T value)
   в ServiceStack.Redis.RedisClientManagerCacheClient.Add[T](String key, T value)
   в loto.ServiceInterface.LotoServices.Post(CreateGame request) в D:\Documents\Visual Studio 2015\Projects\loto\loto\loto.ServiceInterface\LotoServices.cs:строка 42
  1. 消息:整数响应未知回复:422

跟踪:

 в ServiceStack.Redis.RedisNativeClient.ReadLong()
   в ServiceStack.Redis.RedisNativeClient.SendReceive[T](Byte[][] cmdWithBinaryArgs, Func`1 fn, Action`1 completePipelineFn, Boolean sendWithoutRead)
   в ServiceStack.Redis.RedisNativeClient.SendExpectLong(Byte[][] cmdWithBinaryArgs)
   в ServiceStack.Redis.RedisNativeClient.Del(Byte[] key)
   в ServiceStack.Redis.RedisClient.Remove(String key)
   в ServiceStack.Redis.RedisClientManagerCacheClient.Remove(String key)
   в loto.ServiceInterface.GameDispatchService.OnTimedEvent(Object sender, ElapsedEventArgs e) в D:\Documents\Visual Studio 2015\Projects\loto\loto\loto.ServiceInterface\GameDispatchService.cs:строка 67

我有不同的数据所以错误很多

ICacheClient 是一个单例,它不应该被释放,所以你不应该把它放在 using 中,即:

using (var cacheClient = this.GetCacheClient()) {

由于它已注册为 ICacheClient,您可以使用以下方式解决它:

var cacheClient = HostContext.Cache;

这只是一个包装器:

var cacheClient = HostContext.TryResolve<ICacheClient>();