Exemplo n.º 1
0
        /// <summary>
        /// 从缓存获取获取实体
        /// </summary>
        /// <param name="id">主键</param>
        public static BaseAreaEntity GetObjectByCache(string id)
        {
            BaseAreaEntity result = null;

            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            string cacheObject             = "Area";

            if (!string.IsNullOrEmpty(id))
            {
                cacheObject = "Area" + id;
            }
            if (cache != null && cache[cacheObject] == null)
            {
                lock (locker)
                {
                    if (cache != null && cache[cacheObject] == null)
                    {
                        BaseAreaManager manager = new DotNet.Business.BaseAreaManager(BaseAreaEntity.TableName);
                        result = manager.GetObject(id);
                        // 若是空的不用缓存,继续读取实体
                        if (result != null)
                        {
                            cache.Add(cacheObject, result, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.Normal, null);
                            System.Console.WriteLine(System.DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " cache Area " + id);
                        }
                    }
                }
            }
            result = cache[cacheObject] as BaseAreaEntity;
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 从缓存获取获取实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        public static BaseAreaEntity GetObjectByCache(string id)
        {
            BaseAreaEntity result   = null;
            string         cacheKey = "Area:";

            if (!string.IsNullOrEmpty(id))
            {
                cacheKey = cacheKey + id;
            }

            result = GetCache(cacheKey);
            if (result == null || string.IsNullOrWhiteSpace(result.Id))
            {
                BaseAreaManager manager = new DotNet.Business.BaseAreaManager();
                result = manager.GetObject(id);
                // 若是空的不用缓存,继续读取实体
                if (result != null)
                {
                    SetCache(result);
                }
            }

            return(result);
        }