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");
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。