最近在做数据同步,需要将数据库中的数据同步到redis数据库中,客户端使用了ServiceStack.Redis 的类库,具体代码如下
/// <summary> /// 数据插入Redis 数据库 /// </summary> /// <param name="entityList">需要写入得数据集合</param> public void InsertToRedis(IEnumerable<CustTreeModel> entityList) { if (entityList != null && entityList.Any()) { var resultList = ConvertToNoSql(entityList);//数据格式转换 foreach (var result in resultList) { bool isRetry = false; Retry: try { _redisHelpInstance.AddRangeToTwoObject(result.Key, result.Value); } catch (Exception ex) { if (!isRetry) { isRetry = true; goto Retry; } } } } }
插入的方法:
public void AddRangeToTwoObject<T>(string key, Dictionary<string, T> dicResult) { if (dicResult == null || dicResult.Count == 0) return; using (var client = GetClient()) { var typeRedis = client.As<T>(); HashSet<string> keyList=null ; try { keyList = client.GetAllItemsFromSet(key); } catch (Exception ex) { LogHelp.Instance.Write(string.Format("key:{0} {1}", key, ex.ToString()) ,MessageType.Error, GetType(),MethodBase.GetCurrentMethod().Name); } if (keyList != null && keyList.Count > 0) using (var redis = typeRedis.CreatePipeline()) { foreach (string result in keyList) { string entryKey = result; redis.QueueCommand(r => { r.RemoveEntry(entryKey); }); } redis.Flush(); } client.Remove(key); using (var redis = client.CreatePipeline()) { foreach (var result in dicResult) { KeyValuePair<string, T> entity = result; redis.QueueCommand(r => r.AddItemToSet(key, entity.Key)); } redis.Flush(); } if (dicResult.Count > 0) using (var redis = typeRedis.CreatePipeline()) { foreach (var result in dicResult) { KeyValuePair<string, T> entity = result; redis.QueueCommand(r => r.SetEntry(entity.Key, entity.Value)); } redis.Flush(); } Dispose(client); } }
在运行过程中经常会出现错误!错误信息如下,求大神指点
Class Name is EMIncrementEngine.DAL.RealEstateTreeManageDAL Method Name is InsertToRedis Log type is Error Log content is Unknown reply on integer response: 4951000004168787266, sPort: 4854, LastCommand: SADD ids:RealEstateTreeModel 22278460 at ServiceStack.Redis.RedisNativeClient.CreateResponseError(String error) in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\RedisNativeClient_Utils.cs:line 162 at ServiceStack.Redis.RedisNativeClient.ReadLong() in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\RedisNativeClient_Utils.cs:line 552 at ServiceStack.Redis.RedisNativeClient.SendExpectLong(Byte[][] cmdWithBinaryArgs) in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\RedisNativeClient_Utils.cs:line 336 at ServiceStack.Redis.RedisNativeClient.SAdd(String setId, Byte[] value) in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\RedisNativeClient.cs:line 783 at ServiceStack.Redis.RedisClient.AddItemToSet(String setId, String item) in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\RedisClient_Set.cs:line 79 at ServiceStack.Redis.RedisClient.<>c__DisplayClass21.<AddTypeIdsRegisteredDuringPipeline>b__1d(String x) in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\RedisClient.cs:line 547 at ServiceStack.Common.Extensions.EnumerableExtensions.ForEach[T](IEnumerable`1 values, Action`1 action) at ServiceStack.Redis.RedisClient.AddTypeIdsRegisteredDuringPipeline() in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\RedisClient.cs:line 547 at ServiceStack.Redis.Generic.RedisTypedClient`1.AddTypeIdsRegisteredDuringPipeline() in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\Generic\RedisTypedClient.cs:line 137 at ServiceStack.Redis.RedisTypedPipeline`1.Flush() in d:\Projects\MIService\macroservice\src\Source\ServiceStack.Redis\Generic\RedisTypedPipeline.cs:line 48 at EMDBHelp.RedisHelp.AddRangeToTwoObject[T](String key, Dictionary`2 dicResult) in d:\Projects\MIService\macroservice\src\Source\EMDBHelp\RedisHelp.cs:line 400 at EMIncrementEngine.DAL.RealEstateTreeManageDAL.InsertToRedis(IEnumerable`1 entityList) in d:\Projects\MIService\macroservice\src\Source\IncrementServer\EMIncrementEngine\DAL\RealEstateTreeManageDAL.cs:line 85
Class Name is EMDBHelp.RedisHelp Method Name is AddRangeToTwoObject Log type is Error Log content is key:0003001700010025 ServiceStack.Redis.RedisResponseException: Unexpected end of Stream, sPort: 1701, LastCommand: SADD ids:InmrTreeModel 44802832 at ServiceStack.Redis.RedisNativeClient.CreateResponseError(String error) at ServiceStack.Redis.RedisNativeClient.ParseSingleLine(String r) at ServiceStack.Redis.RedisNativeClient.ReadData() at ServiceStack.Redis.RedisNativeClient.ReadMultiData() at ServiceStack.Redis.RedisNativeClient.SendExpectMultiData(Byte[][] cmdWithBinaryArgs) at ServiceStack.Redis.RedisNativeClient.SMembers(String setId) at ServiceStack.Redis.RedisClient.GetAllItemsFromSet(String setId) at EMDBHelp.RedisHelp.AddRangeToTwoObject[T](String key, Dictionary`2 dicResult)
ServiceStack 版本太低了吧,升级一下。
ss驱动的话好像不支持哨兵模式,确定一下你们redis服务部署的是不是哨兵模式。我们已经转战se的驱动了。