• C# Cache缓存读取设置


    先创建一个CacheHelper.cs类,代码如下:

    using System;
    using System.Web;
    using System.Collections;
    using System.Web.Caching;
     
    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 = 7200)
        {
            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());
            }
        }
    }
     
    

    然后是调用:

            public IEnumerable<CompanyModel> FindCompanys()
            {
                var cache = CacheHelper.GetCache("commonData_Company");//先读取
                if (cache == null)//如果没有该缓存
                {
                    var queryCompany = _base.CompanyModel();//从数据库取出
                    var enumerable = queryCompany.ToList();
                    CacheHelper.SetCache("commonData_Company", enumerable);//添加缓存
                    return enumerable;
                }
                var result = (List<CompanyModel>)cache;//有就直接返回该缓存
                return result;
            }
    

     测试结果:

    首次加载进来是为null,然后读取数据库,添加进缓存,当前返回前台的是从数据库中取出的数据。

    刷新页面,发现缓存中已经有了读出的30条数据,

    然后接下来走,返回缓存中的数据:

     

      

  • 相关阅读:
    Visual Studio 2017 RC使用初体验
    ElasticSearch自定义分析器-集成结巴分词插件
    centos右上角wired图标消失有效解决方案
    内聚与耦合
    决策树
    方向导数、梯度、法线间的关系
    解决selenium.common.exception.WebDriverException:Message:'chromedriver' executable needs to be in Path
    算法--近义词反义词
    低代码平台,JeecgBoot v3.0版本发布—新里程牌开始,迎接VUE3版本到来
    低代码报表,JimuReport积木报表 v1.4.0版本发布,免费的可视化数据产品
  • 原文地址:https://www.cnblogs.com/lgx5/p/10785920.html
Copyright © 2020-2023  润新知