ASP.NET 使用HttpContext.Current.Cache

来源:互联网 发布:wampserver是什么软件 编辑:程序博客网 时间:2024/05/16 13:45

public abstract class BizObject
    {
        protected const int MAXROWS = int.MaxValue-1;

        protected static Cache Cache
        {
            get { return HttpContext.Current.Cache; }
        }

             

        //Cache Data
        protected static void CacheData(string key, object data)
        {
            if (Settings.EnableCaching && data != null)
            {
                Cache.Insert(key, data, null, DateTime.Now.AddSeconds(Settings.CacheDuration), TimeSpan.Zero);
            }
        }
   

        /// <summary>
        /// 清除带prefix前缀的缓存
        /// </summary>
        protected static void PurgeCacheItems(string prefix)
        {
            prefix = prefix.ToLower();
            List<string> itemsToRemove = new List<string>();
            IDictionaryEnumerator enumerator = BizObject.Cache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Key.ToString().ToLower().StartsWith(prefix))
                    itemsToRemove.Add(enumerator.Key.ToString());
            }

            foreach (string itemToRemove in itemsToRemove)
                BizObject.Cache.Remove(itemToRemove);
           
        }

原创粉丝点击