public void ImportDataToSet <T>(string key, List <T> value) where T : class, new() { if (value.Count() > 0) { ArrayList arrayList = new ArrayList(); RedisValue[] redisValues = new RedisValue[] { }; foreach (var item in value) { //RedisHelper.SetAddAsync(key, JsonSerializer.SerializeToString(item)).Wait(); //RedisHelper.SetAdd(key, JsonSerializer.SerializeToString(item)); RedisValue redisValue = (RedisValue)JsonSerializer.SerializeToString(item); arrayList.Add(redisValue); } redisValues = (RedisValue[])arrayList.ToArray(typeof(RedisValue)); RedisWriteHelper.SetMutiAdd(key, redisValues); } }
/// <summary> /// 将数据库数据导入到redis /// </summary> /// <typeparam name="T">表类型</typeparam> /// <param name="key">表的model类名</param> /// <param name="value">表记录</param> /// <param name="relationColumns">表外键</param> public void ImportDataToSet <T>(string key, List <T> value, string[] relationColumns, int recordCount) where T : class, new() { if (value.Count() > 0) { ArrayList arrayList = new ArrayList(); RedisValue[] redisValues = new RedisValue[] { }; PropertyInfo property; Type classType = typeof(T); //表属性 PropertyInfo[] propertyInfoArray = classType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); string RedisConditionKeys = "";//查询条件keys int k = 0; Dictionary <string, object> recordDic = null; foreach (var item in value) { //循环表记录 string RedisConditionValus = ""; //查询条件values //给RedisConditionValus字段赋值 property = propertyInfoArray.FirstOrDefault(t => t.Name.ToUpper() == "HOSPITALID"); if (property != null) { object propertyValue = property.GetValue(item); if (propertyValue != null) { RedisConditionValus += propertyValue.ToString() + ","; } if (k == 0) { RedisConditionKeys += "HospitalId,"; } } property = propertyInfoArray.FirstOrDefault(t => t.Name.ToUpper() == "NAME"); if (property != null) { object propertyValue = property.GetValue(item); if (propertyValue != null) { RedisConditionValus += propertyValue.ToString() + ","; } if (k == 0) { RedisConditionKeys += "Name,"; } } property = propertyInfoArray.FirstOrDefault(t => t.Name.ToUpper() == "CODE"); if (property != null) { object propertyValue = property.GetValue(item); if (propertyValue != null) { RedisConditionValus += propertyValue.ToString() + ","; } if (k == 0) { RedisConditionKeys += "Code,"; } } foreach (string relationColumn in relationColumns) { property = propertyInfoArray.FirstOrDefault(t => t.Name.ToUpper() == relationColumn.ToUpper()); if (property != null) { object propertyValue = property.GetValue(item); if (propertyValue != null) { RedisConditionValus += propertyValue.ToString() + ","; } if (k == 0) { RedisConditionKeys += relationColumn + ","; } } } property = propertyInfoArray.FirstOrDefault(t => t.Name.ToUpper() == "ISACTIVE"); if (property != null) { object propertyValue = property.GetValue(item); if (propertyValue != null) { RedisConditionValus += propertyValue.ToString().ToLower() + ","; } if (k == 0) { RedisConditionKeys += "IsActive"; } } if (RedisConditionValus.EndsWith(",")) { RedisConditionValus = RedisConditionValus.Substring(0, RedisConditionValus.Length - 1); } property = classType.GetProperty("RedisConditionValus", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); property.SetValue(item, RedisConditionValus); //去掉带图片的字段 recordDic = new Dictionary <string, object>(); string guid = null; foreach (PropertyInfo propertyInfo in propertyInfoArray) { if (propertyInfo.PropertyType.FullName == "System.Byte[]") { continue; } if (propertyInfo.Name == "GUID") { guid = propertyInfo.GetValue(item).ToString(); } recordDic.Add(propertyInfo.Name, propertyInfo.GetValue(item)); } //string rowsJson = Newtonsoft.Json.JsonConvert.SerializeObject(recordDic); //RedisWriteHelper.SetHash(key, guid, rowsJson); RedisValue redisValue = (RedisValue)JsonSerializer.SerializeToString(recordDic); arrayList.Add(redisValue); k++; } if (arrayList.Count > 30000) { ArrayList arrayListPartOne = arrayList.GetRange(0, 20000); ArrayList arrayListPartTwo = arrayList.GetRange(20000, arrayList.Count - 20000); redisValues = (RedisValue[])arrayListPartOne.ToArray(typeof(RedisValue)); RedisWriteHelper.SetMutiAdd(key, redisValues); redisValues = (RedisValue[])arrayListPartTwo.ToArray(typeof(RedisValue)); RedisWriteHelper.SetMutiAdd(key, redisValues); } else { redisValues = (RedisValue[])arrayList.ToArray(typeof(RedisValue)); RedisWriteHelper.SetMutiAdd(key, redisValues); } if (RedisConditionKeys != "") { if (RedisConditionKeys.EndsWith(",")) { RedisConditionKeys = RedisConditionKeys.Substring(0, RedisConditionKeys.Length - 1); } RedisWriteHelper.SetHash <T>(RedisDefaultKey, key, RedisConditionKeys); } } }