Exemplo n.º 1
0
        /// <summary>获取调用者</summary>
        /// <returns></returns>
        public static String GetCaller(Int32 maxNum = 10)
        {
            return(XTrace.GetCaller(2, maxNum, "<-"));
            //var st = new StackTrace(2, true);
            //var sb = new StringBuilder();
            //int count = Math.Min(maxNum, st.FrameCount);
            //Type last = null;
            //var asm = Assembly.GetEntryAssembly();
            //var entry = asm == null ? null : asm.EntryPoint;
            //for (int i = 0; i < count; i++)
            //{
            //    var sf = st.GetFrame(i);
            //    var method = sf.GetMethod();

            //    var name = method.ToString();
            //    // 去掉前面的返回类型
            //    if (name.Contains(" ")) name = name.Substring(name.IndexOf(" ") + 1);

            //    var type = method.DeclaringType ?? method.ReflectedType;
            //    if (type != null && type != last)
            //        sb.AppendFormat("{0}.{1}", TypeX.Create(type).Name, name);
            //    else
            //        sb.AppendFormat("{0}", name);

            //    if (i < count - 1) sb.Append("<-");

            //    last = type;

            //    // 如果到达了入口点,可以结束了
            //    if (method == entry) break;
            //}
            //return sb.ToString();
        }
Exemplo n.º 2
0
        Task UpdateCacheAsync()
        {
            // 这里直接计算有效期,避免每次判断缓存有效期时进行的时间相加而带来的性能损耗
            // 设置时间放在获取缓存之前,让其它线程不要空等
            ExpiredTime = DateTime.Now.AddSeconds(Expire);
            Times++;

            if (Debug)
            {
                DAL.WriteLog("{0}", XTrace.GetCaller(3, 16));
            }

            return(Task.Factory.StartNew(FillWaper, Times));
        }
Exemplo n.º 3
0
        /// <summary>尝试向两个字典加入数据</summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private Boolean TryAdd(TEntity entity)
        {
            if (!Using)
            {
                Using = true;
                if (Debug)
                {
                    DAL.WriteLog("单对象缓存首次使用 {0} {1}", typeof(TEntity).FullName, XTrace.GetCaller(1, 16));
                }
            }

            var item = new CacheItem {
                sc = this
            };

            String slaveKey = null;

            if (GetSlaveKeyMethod != null)
            {
                slaveKey = GetSlaveKeyMethod(entity);
            }

            var mkey = GetKeyMethod(entity);

            item.Key      = mkey;
            item.SlaveKey = slaveKey;
            item.SetEntity(entity);

            var success = false;

            lock (Entities)
            {
                if (!Entities.ContainsKey(mkey))
                {
                    Entities.Add(mkey, item);
                    success = true;
                }
            }
            if (success && !slaveKey.IsNullOrWhiteSpace())
            {
                lock (SlaveEntities)
                {
                    // 新增或更新
                    SlaveEntities[slaveKey] = item;
                }
            }
            return(success);
        }
Exemplo n.º 4
0
        void UpdateCache(Boolean nodata)
        {
            // 异步更新时,如果为空,表明首次,同步获取数据
            // 有且仅有非首次且数据不为空时执行异步查询
            if (Times > 0 && Asynchronous && !nodata)
            {
                // 这里直接计算有效期,避免每次判断缓存有效期时进行的时间相加而带来的性能损耗
                // 设置时间放在获取缓存之前,让其它线程不要空等
                ExpiredTime = DateTime.Now.AddSeconds(Expire);
                Times++;

                if (Debug)
                {
                    var reason = Times == 1 ? "第一次" : (nodata ? "无缓存数据" : Expire + "秒过期");
                    DAL.WriteLog("异步更新实体缓存(第{2}次):{0} 原因:{1} {3}", typeof(TEntity).FullName, reason, Times, XTrace.GetCaller(3, 16));
                }

                Task.Factory.StartNew(FillWaper, Times);
            }
            else
            {
                Times++;
                if (Debug)
                {
                    var reason = Times == 1 ? "第一次" : (nodata ? "无缓存数据" : Expire + "秒过期");
                    DAL.WriteLog("更新实体缓存(第{2}次):{0} 原因:{1} {3}", typeof(TEntity).FullName, reason, Times, XTrace.GetCaller(3, 16));
                }

                FillWaper(Times);

                // 这里直接计算有效期,避免每次判断缓存有效期时进行的时间相加而带来的性能损耗
                // 设置时间放在获取缓存之后,避免缓存尚未拿到,其它线程拿到空数据
                ExpiredTime = DateTime.Now.AddSeconds(Expire);
            }
        }