public static BaseOrganizeEntity GetObjectByCache(string id, bool fefreshCache = false) { BaseOrganizeEntity result = null; if (!string.IsNullOrEmpty(id)) { string cacheKey = "O:"; cacheKey = cacheKey + id; if (!fefreshCache) { result = GetCacheByKey(cacheKey); } if (result == null) { BaseOrganizeManager manager = new DotNet.Business.BaseOrganizeManager(); result = manager.GetObject(id); // 若是空的不用缓存,继续读取实体 if (result != null) { SetCache(result); } } } return(result); }
/// <summary> /// 从缓存获取获取实体 /// </summary> /// <param name="id">主键</param> /// <returns>实体</returns> public static BaseOrganizeEntity GetObjectByCache(IRedisClient redisClient, string id) { BaseOrganizeEntity result = null; if (!string.IsNullOrEmpty(id)) { //string cacheKey = "Organize"; string cacheKey = "O:"; cacheKey = cacheKey + id; result = GetCacheByKey(redisClient, cacheKey); if (result == null) { BaseOrganizeManager manager = new DotNet.Business.BaseOrganizeManager(); result = manager.GetObject(id); // 若是空的不用缓存,继续读取实体 if (result != null) { SetCache(result); } } } return(result); }
public static List <BaseOrganizeEntity> GetTransferCenterByCache() { List <BaseOrganizeEntity> result = null; System.Web.Caching.Cache cache = HttpRuntime.Cache; string cacheObject = "TransferCenter"; if (cache != null && cache[cacheObject] == null) { BaseOrganizeManager manager = new DotNet.Business.BaseOrganizeManager(); result = manager.GetTransferCenter(); // 若是空的不用缓存,继续读取实体 if (result != null) { cache.Add(cacheObject, result, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Normal, null); System.Console.WriteLine(System.DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " cache TransferCenter "); } } result = cache[cacheObject] as List <BaseOrganizeEntity>; return(result); }
/* * /// <summary> * /// 从缓存获取获取实体 * /// </summary> * /// <param name="id">主键</param> * public static BaseOrganizeEntity GetObjectByCache(string id) * { * BaseOrganizeEntity result = null; * System.Web.Caching.Cache cache = HttpRuntime.Cache; * string cacheObject = "Organize"; * if (!string.IsNullOrEmpty(id)) * { * cacheObject = "Organize" + id; * } * if (cache != null && cache[cacheObject] == null) * { * lock (locker) * { * if (cache != null && cache[cacheObject] == null) * { * BaseOrganizeManager manager = new DotNet.Business.BaseOrganizeManager(BaseOrganizeEntity.TableName); * result = manager.GetObject(id); * // 若是空的不用缓存,继续读取实体 * if (result != null) * { * cache.Add(cacheObject, result, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Normal, null); * System.Console.WriteLine(System.DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " cache Organize " + id); * } * } * } * } * result = cache[cacheObject] as BaseOrganizeEntity; * return result; * } * * public static BaseOrganizeEntity GetObjectByCodeByCache(string code) * { * BaseOrganizeEntity result = null; * System.Web.Caching.Cache cache = HttpRuntime.Cache; * string cacheObject = "OrganizeByCode" + code; * if (cache != null && cache[cacheObject] == null) * { * lock (locker) * { * if (cache != null && cache[cacheObject] == null) * { * BaseOrganizeManager organizeManager = new BaseOrganizeManager(); * result = organizeManager.GetObjectByCode(code); * cache.Add(cacheObject, result, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Normal, null); * System.Console.WriteLine(System.DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " cache Organize"); * } * } * } * result = cache[cacheObject] as BaseOrganizeEntity; * return result; * } * * public static BaseOrganizeEntity GetObjectByNameByCache(string fullName) * { * BaseOrganizeEntity result = null; * System.Web.Caching.Cache cache = HttpRuntime.Cache; * string cacheObject = "OrganizeByName" + fullName; * if (cache != null && cache[cacheObject] == null) * { * lock (locker) * { * if (cache != null && cache[cacheObject] == null) * { * BaseOrganizeManager organizeManager = new BaseOrganizeManager(); * result = organizeManager.GetObjectByName(fullName); * cache.Add(cacheObject, result, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Normal, null); * System.Console.WriteLine(System.DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " cache Organize"); * } * } * } * result = cache[cacheObject] as BaseOrganizeEntity; * return result; * } */ #region public static List<BaseOrganizeEntity> GetEntities() 获取角色表,从缓存读取 /// <summary> /// 获取角色表,从缓存读取 /// </summary> public static List <BaseOrganizeEntity> GetEntities() { System.Web.Caching.Cache cache = HttpRuntime.Cache; if (cache != null && cache[BaseOrganizeEntity.TableName] == null) { lock (BaseSystemInfo.UserLock) { if (cache != null && cache[BaseOrganizeEntity.TableName] == null) { // 读取目标表中的数据 List <BaseOrganizeEntity> entityList = null; BaseOrganizeManager manager = new DotNet.Business.BaseOrganizeManager(BaseOrganizeEntity.TableName); entityList = manager.GetList <BaseOrganizeEntity>(); // 这个是没写过期时间的方法 // HttpContext.Current.Cache[tableName] = entityList; // 设置过期时间为8个小时,第2天若有不正常的自动就可以正常了 cache.Add(BaseOrganizeEntity.TableName, entityList, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Normal, null); } } } return(cache[BaseOrganizeEntity.TableName] as List <BaseOrganizeEntity>); }