Asp.Net Cache輔助類

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   檔案   

輔助類代碼

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Caching;namespace BusinessRules.Common{    /// <summary>    /// Cache高度緩衝    /// </summary>    public class CacheManager    {        #region --Property--        private static string _cacheKey = String.Empty;        private static String _sqlDepName = "CacheData";        /// <summary>        /// 緩衝索引索引值        /// </summary>        public static string CacheKey        {            get { return CacheManager._cacheKey; }            set { CacheManager._cacheKey = value; }        }        /// <summary>        /// 緩衝依賴名稱        /// </summary>        public static String SqlDepName        {            get { return CacheManager._sqlDepName; }            set { CacheManager._sqlDepName = value; }        }        #endregion        #region --Method--        /// <summary>        /// 擷取當前應用程式指定cacheKey的Cache對象值        /// </summary>        /// <param name="cacheKety">索引索引值</param>        /// <returns>返回對象</returns>        public static Object GetCache(String cacheKey)        {            if (!String.IsNullOrEmpty(cacheKey))            {                Cache objCache = HttpRuntime.Cache;                return objCache[cacheKey];            }            else            {                return null;            }        }        /// <summary>        /// 擷取當前應用程式指定cacheKey的Cache對象值        /// </summary>        /// <typeparam name="T">物件類型</typeparam>        /// <param name="cacheKey">索引索引值</param>        /// <returns>返回物件類型</returns>        public static T GetCache<T>(String cacheKey)        {            object obj = GetCache(cacheKey);            return obj == null ? default(T) : (T)obj;        }        /// <summary>        /// 設定當前應用程式指定CacheKey的Cache對象值        /// </summary>        /// <param name="cacheKey">索引索引值</param>        /// <param name="obj">c</param>        public static void SetCache(String cacheKey, Object obj)        {            Cache objCache = HttpRuntime.Cache;            objCache.Insert(cacheKey, obj);        }        /// <summary>        /// 建立緩衝依賴項        /// </summary>        /// <param name="cacheKey"></param>        /// <param name="obj"></param>        /// <param name="fileName"></param>        public static void SetCache(String cacheKey, Object obj, String fileName)        {            CacheDependency cdep = new CacheDependency(fileName);            Cache objCache = HttpRuntime.Cache;            objCache.Insert(cacheKey, obj, cdep);        }        /// <summary>        /// 設定當前應用程式指定CacheKey的Cache對象值        /// </summary>        /// <param name="cacheKey">索引索引值</param>        /// <param name="obj">索引索引值</param>        /// <param name="absoluteExpiration">絕對到期時間</param>        /// <param name="slidingExpiration">最後一次訪問所插入對象時與該對象到期時之間的時間間隔</param>        public static void SetCache(String cacheKey, Object obj, DateTime absoluteExpiration, TimeSpan slidingExpiration)        {            Cache objCache = HttpRuntime.Cache;            objCache.Insert(cacheKey, obj, null, absoluteExpiration, slidingExpiration);        }        /// <summary>        /// 建立快取項目到期        /// </summary>        /// <param name="cacheKey">索引索引值</param>        /// <param name="obj">索引對象</param>        /// <param name="expires">到期時間</param>        public static void SetCache(String cacheKey, Object obj, Int32 expires)        {            Cache objCache = HttpRuntime.Cache;            objCache.Insert(            cacheKey,            obj,            null,            Cache.NoAbsoluteExpiration,//從不到期            new TimeSpan(0, expires, 0)            );        }        /// <summary>        /// 設定依賴的方式快取資料        /// </summary>        /// <param name="cacheKey"></param>        /// <param name="obj"></param>        /// <param name="cdep"></param>        public static void SetCache(String cacheKey, Object obj, CacheDependency cdep)        {            Cache objCache = HttpRuntime.Cache;            objCache.Insert(                cacheKey,                obj,                cdep,                Cache.NoAbsoluteExpiration,//從不到期                Cache.NoSlidingExpiration,//禁用可調到期                CacheItemPriority.Default,                null                );        }        //==========調用方式Start===============        //    檔案依賴使用說明        //    System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency("C:\\test.txt");        //   SetCache(CacheKey, objModel, dep);//寫入緩衝        //        //  資料庫依賴項使用說明        //   System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("codematic", "P_Product");        //  SetCache(CacheKey, objModel, dep);//寫入緩衝        //        //==========調用方式end===============        #endregion    }}

調用方式

 

        //設定緩衝的索引值            CacheManager.CacheKey = "NavMenue";            //擷取當前索引值下的快取資料            Object cacheModel = CacheManager.GetCache(CacheManager.CacheKey);            //如果緩衝中不存在當前索引值對象            if (cacheModel == null)            {                //將資料存放區到緩衝中                cacheModel = bsnManage.Obj_List(navPosition);                if (cacheModel != null)                {                    //依賴資料庫的sc_navigation 表變化來更新                    SqlCacheDependency scdep = new SqlCacheDependency(CacheManager.SqlDepName, "sc_navigation");                    //寫入緩衝                    CacheManager.SetCache(CacheManager.CacheKey, cacheModel, scdep);                }                return (DataTable)cacheModel;            }            else                return (DataTable)CacheManager.GetCache(CacheManager.CacheKey);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.