码迷,mamicode.com
首页 > Web开发 > 详细

.Net Cache

时间:2017-03-02 23:52:25      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:timeout   技术   alt   ret   上下   isp   tar   str   委托   

在.net中有两个类实现了Cache

  • HttpRuntime.Cache 应该程序使用的Cache,web也可以用
  • HttpContext.Current.Cache  web上下文的Cache对象,只能在Web上使用

 

asp.net 页面缓存的封装

技术分享
  /// <summary>
        /// 根据 cacheKey 获取缓存内容
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="cacheName">key</param>
        /// <param name="cacheFunc">Func委托,该方法返回结果为要缓存的对象,当存在改缓存时,不会执行该委托的方法</param>
        /// <param name="timeoutHours">单位:小时,默认值为12</param>
        /// <returns>缓存内容</returns>
        public static Object GetCacheByName(HttpContextBase context,string cacheName,Func<object>cacheFunc,int timeoutHours=12)
        {
            if (context.Cache[cacheName] == null)
            {
                context.Cache.Insert(cacheName, cacheFunc.Invoke(), null, DateTime.Now.AddHours(timeoutHours), Cache.NoSlidingExpiration);
            }
            return context.Cache[cacheName];
        }
View Code

 

.Net Cache

标签:timeout   技术   alt   ret   上下   isp   tar   str   委托   

原文地址:http://www.cnblogs.com/Blogs-Wang/p/6486832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!