C#運用Cache緩存基礎數據,防止多次讀取數據庫

1、首先是Cache幫助類

 public class CacheHelper
    {
        /// <summary>
        /// 獲取數據緩存
        /// </summary>
        /// <param name="cacheKey">鍵</param>
        public static object GetCache(string cacheKey)
        {
            var objCache = HttpRuntime.Cache.Get(cacheKey);
            return objCache;
        }
        /// <summary>
        /// 設置數據緩存
        /// </summary>
        public static void SetCache(string cacheKey, object objObject)
        {
            var objCache = HttpRuntime.Cache;
            objCache.Insert(cacheKey, objObject);
        }
        /// <summary>
        /// 設置數據緩存
        /// </summary>
        public static void SetCache(string cacheKey, object objObject, int timeout = 24*60*60)
        {
            try
            {
                if (objObject == null) return;
                var objCache = HttpRuntime.Cache;
                //相對過期
                //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
                //絕對過期時間
                objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);
            }
            catch (Exception)
            {
                //throw;
            }
        }
        /// <summary>
        /// 移除指定數據緩存
        /// </summary>
        public static void RemoveAllCache(string cacheKey)
        {
            var cache = HttpRuntime.Cache;
            cache.Remove(cacheKey);
        }
        /// <summary>
        /// 移除全部緩存
        /// </summary>
        public static void RemoveAllCache()
        {
            var cache = HttpRuntime.Cache;
            var cacheEnum = cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                cache.Remove(cacheEnum.Key.ToString());
            }
        }

    }

2、實現部分代碼

 public static class DictionaryHelper
 {
        /// <summary>
        /// 獲取所有的字典
        /// </summary>
        /// <param name="DicName"></param>
        /// <returns></returns>
        public static Dictionary<string,string> FindCommonDictionary(string DicName)
        {
            try
            {
                var cache = CacheHelper.GetCache("commonData_Dictionary");//先讀取
                if (cache == null)//如果沒有該緩存
                {
                    var queryCommon = service.GetS_DICTIONARY_COMMONs();//從數據庫取出
                    CacheHelper.SetCache("commonData_Dictionary", queryCommon);//添加緩存
                    return queryCommon[DicName];  //根據字典名字取出對應的字典
                }
                var result = (Dictionary<string, Dictionary<string, string>>)cache;//有就直接返回該緩存
                return result[DicName];//根據字典名字取出對應的字典
            }
            catch (Exception ex)
            {
                return null;         //沒有該字典返回空
            }
           
        }
      }

3、當新增更新刪除字典的時候要清除緩存中的數據,重新加載

 CacheHelper.RemoveAllCache("commonData_Dictionary");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章