/// <summary> /// 缓存获取 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <param name="time">缓存时间 单位分钟</param> /// <returns>返回调用结果集合</returns> public static TResult GetObjectByMin <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, int minute, bool insertToDB) { object result = null; int requestTimes = 0; while (requestTimes < 3)//取三次,防止取缓存时在Set,取不到数据 { result = HHMemCachedDllImport.Get(key, 0, minute, insertToDB); if (result == null) { Thread.Sleep(20); } else { break; } requestTimes++; } if (result == null) { result = callGetLogic(callEntity); if (result != null) { string jsonCallEnity = ConvertEnityToJson(callEntity); HHMemCachedDllImport.Set(key, result, 0, minute, jsonCallEnity, insertToDB); } } return((TResult)result); }
/// <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> /// 缓存获取 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <param name="time">缓存时间 单位分钟</param> /// <returns>返回调用结果集合</returns> public static TResult GetObjectByTimeSpan <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, TimeSpan timeSpan) { //关闭请开启以下语句 //return callGetLogic(callEntity); object result = null; int requestTimes = 0; while (requestTimes < 2)//取2次,防止取缓存时在Set,取不到数据 { result = HHMemCachedDllImport.Get(key); if (result == null) { Thread.Sleep(10); } else { break; } requestTimes++; } if (result == null) { result = callGetLogic(callEntity); if (result != null) { string jsonCallEnity = ConvertEnityToJson(callEntity); HHMemCachedDllImport.Set(key, result, timeSpan, jsonCallEnity, true); } } return((TResult)result); }
/// <summary> /// 缓存获取 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <returns>返回调用结果集合</returns> public static TResult GetObject <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity) { //Added by Pluto Mei var tResult = HHMemCachedDllImport.Get(key); TResult result = default(TResult); if (tResult == null) { tResult = callGetLogic(callEntity); if (result != null) { string jsonCallEnity = ConvertEnityToJson(callEntity); HHMemCachedDllImport.Set(key, tResult, int.MaxValue, jsonCallEnity); } } else { result = (TResult)tResult; } return(result); //如果返回类型为值类型,在第一次初始化的时候下面方法将会引发报错 //note by Pluto Mei 2014-2-24 //TResult result = (TResult)HHMemCachedDllImport.Get(key); //if (result == null) //{ // result = callGetLogic(callEntity); // if (result != null) // { // HHMemCachedDllImport.Set(key, result); // } //} //return result; }
/// <summary> /// 缓存获取-可实现整取切片缓存 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <param name="time">缓存时间 单位小时</param> /// <returns>返回调用结果集合</returns> public static object GetMergeObject <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, int time) { object keyCount = HHMemCachedDllImport.Get(key, time); object[] result = null; if (keyCount == null || !(keyCount is int) || Convert.ToInt32(keyCount) == 0)//不存在缓存 { result = callGetLogic(callEntity) as object[]; if (result != null) { 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); } HHMemCachedDllImport.Set(key, result.Length, time, jsonCallEnity);//只保存到条件Key的字段中 } } else { List <object> cacheObjectList = new List <object>(); int count = Convert.ToInt32(keyCount); for (int i = 0; i < count; i++) { string itemKey = key + PAGE_SPLIT_CHAR + i.ToString(); if (!CacheManager.IsExit(itemKey)) //缺失一个Key,全部重新Get { if (result == null) //如果不为NULL,表示之前有发生缺失,已经取过接口,将不再重新调用SOA { result = callGetLogic(callEntity) as object[]; } if (result != null && i < result.Length)//不为空,且页码在result的结果集中 { string jsonCallEnity = ConvertEnityToJson(callEntity); object soaItem = result[i]; HHMemCachedDllImport.Set(itemKey, soaItem, time, string.Empty); cacheObjectList.Add(soaItem); //添加到退回列表中 HHMemCachedDllImport.Set(key, result.Length, time, jsonCallEnity); //主Key更新到最新时间 } } else { object item = HHMemCachedDllImport.Get(itemKey, time); cacheObjectList.Add(item); } } result = cacheObjectList.ToArray(); } return(result); }
/// <summary> /// 缓存获取 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <param name="time">缓存时间 单位小时</param> /// <returns>返回调用结果集合</returns> public static TResult GetObject <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, int time) { object result = HHMemCachedDllImport.Get(key, time); if (result == null) { result = callGetLogic(callEntity); if (result != null) { string jsonCallEnity = ConvertEnityToJson(callEntity); HHMemCachedDllImport.Set(key, result, time, jsonCallEnity); } } return((TResult)result); }
/// <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> /// 缓存设置 added by Pluto Mei 2014.2.24 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key">缓存KEY</param> /// <param name="value">缓存值</param> public static void SetObject <T>(string key, T value) { HHMemCachedDllImport.Set(key, value, int.MaxValue, ""); }
/// <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); }
/// <summary> /// 缓存获取 /// </summary> /// <param name="key">缓存KEY</param> /// <param name="callGetLogic">调用逻辑</param> /// <param name="time">缓存时间 单位分钟</param> /// <returns>返回调用结果集合</returns> public static object GetMergeObjectByMin <T, TResult>(string key, Func <T, TResult> callGetLogic, T callEntity, int minute) { object keyCount = null; int requestTimes = 0; while (requestTimes < 3)//取三次,防止取缓存时在Set,取不到数据 { keyCount = HHMemCachedDllImport.Get(key, 0, minute, true); if (keyCount == null) { Thread.Sleep(20); } else { break; } requestTimes++; } object[] result = null; if (keyCount == null || !(keyCount is int) || Convert.ToInt32(keyCount) == 0)//不存在缓存 { result = callGetLogic(callEntity) as object[]; if (result != null) { string jsonCallEnity = ConvertEnityToJson(callEntity); for (int i = 0; i < result.Length; i++) { HHMemCachedDllImport.Set(key + PAGE_SPLIT_CHAR + i.ToString(), result[i], 0, minute, string.Empty, true); } HHMemCachedDllImport.Set(key, result.Length, 0, minute, jsonCallEnity, true); } } else { List <object> cacheObjectList = new List <object>(); int count = Convert.ToInt32(keyCount); for (int i = 0; i < count; i++) { string itemKey = key + PAGE_SPLIT_CHAR + i.ToString(); object item = null; requestTimes = 0; while (requestTimes < 3)//取三次,防止取缓存时在Set,取不到数据 { item = HHMemCachedDllImport.Get(itemKey, 0, minute, true); if (item == null) { Thread.Sleep(20); } else { break; } requestTimes++; } if (item == null) //缺失一个Key,全部重新Get { if (result == null) //如果不为NULL,表示之前有发生缺失,已经取过接口,将不再重新调用SOA { result = callGetLogic(callEntity) as object[]; } if (result != null && i < result.Length)//不为空,且页码在result的结果集中 { string jsonCallEnity = ConvertEnityToJson(callEntity); object soaItem = result[i]; HHMemCachedDllImport.Set(itemKey, soaItem, 0, minute, string.Empty, true); cacheObjectList.Add(soaItem); //添加到退回列表中 HHMemCachedDllImport.Set(key, result.Length, 0, minute, jsonCallEnity, true); //主Key更新到最新时间 } } else { cacheObjectList.Add(item); } } result = cacheObjectList.ToArray(); } return(result); }
public static void SetObject <T>(string key, T value, int min, bool intodb) { HHMemCachedDllImport.Set(key, value, TimeSpan.FromMinutes(min), "", intodb); }