/// <summary> /// 缓存获取 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <param name="time">缓存时间 单位小时</param> /// <returns>返回调用结果集合</returns> public static ByteSliceEntity GetByteSliceEntity <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, int time, bool isForceCall = false) { ByteSliceEntity result = null; if (isForceCall)//强制调用时(Job调用),不计数 { result = callGetLogic(callEntity) as ByteSliceEntity; if (result != null && result.CallSuccess) { string jsonCallEnity = ConvertEnityToJson(callEntity); HHMemCachedDllImport.Set(key, result, time, jsonCallEnity); } } else { result = HHMemCachedDllImport.Get(key, time) as ByteSliceEntity; if (result == null) { result = callGetLogic(callEntity) as ByteSliceEntity; if (result != null && result.CallSuccess) { string jsonCallEnity = ConvertEnityToJson(callEntity); HHMemCachedDllImport.Set(key, result, time, jsonCallEnity); } } else//来自缓存的标记 { result.IsGetFromCache = true; } } return(result); }
/// <summary> /// 把返回实体转换成ByteSliceEntity实体 /// </summary> /// <param name="returnEntity"></param> /// <returns></returns> public static ByteSliceEntity GetByteSliceEntity(BaseReturnEntity returnEntity) { ByteSliceEntity returnByteSliceEntity = null; if (returnEntity != null) { returnEntity.ResponseXML = string.Empty; DataEntityBinarySerialize dataEntityBinarySerialize = new DataEntityBinarySerialize(returnEntity.GetType()); byte[] entityBytes = dataEntityBinarySerialize.Serialize(returnEntity); returnByteSliceEntity = new ByteSliceEntity(returnEntity.CallSuccess, entityBytes.Length, entityBytes); } return(returnByteSliceEntity); }
/// <summary> /// 缓存获取 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <param name="time">缓存时间 单位分钟</param> /// <returns>返回调用结果集合</returns> public static ByteSliceEntity GetByteSliceEntityByMin <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, int minute, bool isForceCall = false) { ByteSliceEntity result = null; if (isForceCall)//强制调用时(Job调用),不计数 { result = callGetLogic(callEntity) as ByteSliceEntity; if (result != null && result.CallSuccess) { string jsonCallEnity = ConvertEnityToJson(callEntity); HHMemCachedDllImport.Set(key, result, 0, minute, jsonCallEnity, true); } } else { int requestTimes = 0; while (requestTimes < 3)//取三次,防止取缓存时在Set,取不到数据 { result = HHMemCachedDllImport.Get(key, 0, minute, true) as ByteSliceEntity; if (result == null) { Thread.Sleep(20); } else { break; } requestTimes++; } if (result == null) { result = callGetLogic(callEntity) as ByteSliceEntity; if (result != null && result.CallSuccess) { string jsonCallEnity = ConvertEnityToJson(callEntity); HHMemCachedDllImport.Set(key, result, 0, minute, jsonCallEnity, true); } } else//来自缓存的标记 { result.IsGetFromCache = true; } } return(result); }
/// <summary> /// 缓存获取-ByteSliceEntity /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <returns>返回调用结果集合</returns> public static ByteSliceEntity GetByteSliceEntity <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, bool isForceCall = false) { ByteSliceEntity result = HHMemCachedDllImport.Get(key) as ByteSliceEntity; if (result == null || isForceCall) { result = callGetLogic(callEntity) as ByteSliceEntity; if (result != null && result.CallSuccess) { HHMemCachedDllImport.Set(key, result); } } else//来自缓存的标记 { result.IsGetFromCache = true; } return(result); }
/// <summary> /// 把返回实体以指定长度(字节)进行分割 /// </summary> /// <param name="returnEntity"></param> /// <param name="sliceLength"></param> public static List <ByteSliceEntity> SplitReturnEntity(BaseReturnEntity returnEntity, int splitLength) { List <ByteSliceEntity> reutnEntityList = new List <ByteSliceEntity>(); if (returnEntity != null) { //去除ResponseXML测试字段 returnEntity.ResponseXML = string.Empty; DataEntityBinarySerialize dataEntityBinarySerialize = new DataEntityBinarySerialize(returnEntity.GetType()); byte[] entityBytes = dataEntityBinarySerialize.Serialize(returnEntity); int sliceLength = splitLength; //切片长度 int entityLenght = entityBytes.Length; //实体总长度 int pageCount = entityLenght / sliceLength + ((entityLenght % sliceLength) > 0 ? 1 : 0); //切片总个数 for (int i = 0; i < pageCount; i++) //拆分字节数据 { ByteSliceEntity siliceItem = new ByteSliceEntity(); if (i == 0)//调用是否成功和总长度放到第一个ByteSliceEntity的 { siliceItem.CallSuccess = returnEntity.CallSuccess; siliceItem.EntityLength = entityBytes.Length; } int copyStartIndex = i * sliceLength; int copyLength = sliceLength; copyLength = (copyStartIndex + copyLength) > entityLenght ? entityLenght - copyStartIndex : copyLength; byte[] content = new byte[copyLength]; Array.Copy(entityBytes, copyStartIndex, content, 0, copyLength);//拷贝到返回流中 siliceItem.Content = content; reutnEntityList.Add(siliceItem); Thread.Sleep(1); } } return(reutnEntityList); }
/// <summary> /// 缓存获取-可实现整取切片缓存 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <param name="time">缓存时间 单位小时</param> /// <returns>返回调用结果集合</returns> public static ByteSliceEntity[] GetMergeByteSliceEntity <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, int time, bool isForceCall = false) { ByteSliceEntity[] result = null; if (isForceCall) { result = callGetLogic(callEntity) as ByteSliceEntity[]; if (result != null && result.Length > 0 && result[0].CallSuccess) { string jsonCallEnity = ConvertEnityToJson(callEntity); for (int i = 0; i < result.Length; i++) { HHMemCachedDllImport.Set(key + PAGE_SPLIT_CHAR + i.ToString(), result[i], time, string.Empty); Thread.Sleep(0); } HHMemCachedDllImport.Set(key, result.Length, time, jsonCallEnity); } } else { object keyCount = HHMemCachedDllImport.Get(key, time); if (keyCount == null || !(keyCount is int) || Convert.ToInt32(keyCount) == 0)//不存在缓存 { result = callGetLogic(callEntity) as ByteSliceEntity[]; if (result != null && result.Length > 0 && result[0].CallSuccess) { string jsonCallEnity = ConvertEnityToJson(callEntity); for (int i = 0; i < result.Length; i++) { HHMemCachedDllImport.Set(key + PAGE_SPLIT_CHAR + i.ToString(), result[i], time, string.Empty); Thread.Sleep(0); } HHMemCachedDllImport.Set(key, result.Length, time, jsonCallEnity); } } else { List <ByteSliceEntity> cacheObjectList = new List <ByteSliceEntity>(); int count = Convert.ToInt32(keyCount); for (int i = 0; i < count; i++) { string itemKey = key + PAGE_SPLIT_CHAR + i.ToString(); ByteSliceEntity item = HHMemCachedDllImport.Get(itemKey, time) as ByteSliceEntity; if (item == null)//缺失一个Key,全部重新Get { cacheObjectList.Clear(); result = callGetLogic(callEntity) as ByteSliceEntity[]; if (result != null && i < result.Length && result[0].CallSuccess)//不为空,且页码在result的结果集中 { string jsonCallEnity = ConvertEnityToJson(callEntity); for (int j = 0; j < result.Length; j++) { itemKey = key + PAGE_SPLIT_CHAR + j.ToString(); ByteSliceEntity soaItem = result[j]; HHMemCachedDllImport.Set(itemKey, soaItem, 0, time, string.Empty, true); cacheObjectList.Add(soaItem);//添加到返回列表中 Thread.Sleep(0); } HHMemCachedDllImport.Set(key, result.Length, time, jsonCallEnity);//主Key更新到最新时间 break; } } else { item.IsGetFromCache = true; cacheObjectList.Add(item); } } result = cacheObjectList.ToArray(); } } return(result); }