把老赵的页面缓存片断改一下,呵呵

老赵同志写的页面缓存片断不错,用着方便,但我感觉在前端调用上有些不便,可以我把他的代码又改了一下,呵呵!

老赵代码的调用:

Before Rendering:
<%= DateTime.Now %>

<br />

Rendering:
<%= Html.Cache("Now", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration,
    () => { System.Threading.Thread.Sleep(5000); return DateTime.Now; }) %>

<br />

After Rendering:
<%= DateTime.Now %>

占占页面片断缓存的调用:

@Html.Cache("test", DateTime.Now.AddMinutes(1),
 @<span>
     @DateTime.Now
 </span>
    );

前台UI层使用了razor视图引擎,写法上很简介,它与C#方法容为一体,我的HTML代码以参数的形式传递给了Cache方法,再看一下Cache方法原型:

    /// <summary>
    /// 缓存的扩展方法
    /// </summary>
    public static class CacheExtensions
    {
        /// <summary>
        /// 对页面中指定字符串进行缓存
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="cacheKey">缓存Key</param>
        /// <param name="cacheDependencies">缓存依附对象,可以为null</param>
        /// <param name="absoluteExpiration">过期时间</param>
        /// <param name="slidingExpiration">相对本次过期时间</param>
        /// <param name="func">要缓存对象</param>
        /// <returns></returns>
        public static HelperResult Cache(this HtmlHelper htmlHelper, string cacheKey, CacheDependency cacheDependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, Func<object, HelperResult> func)
        {
            var cache = htmlHelper.ViewContext.HttpContext.Cache;
            var content = cache.Get(cacheKey) as string;

            if (content == null)
            {
                content = func.Invoke(null).ToHtmlString();
                cache.Insert(cacheKey, content, cacheDependencies, absoluteExpiration, slidingExpiration);
            }

            return new HelperResult(writer =>
            {
                writer.Write(content);
            });
        }

        /// <summary>
        /// 对页面中指定字符串进行缓存
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="cacheKey">缓存Key</param>
        /// <param name="absoluteExpiration">过期时间</param>
        /// <param name="slidingExpiration">相对本次的过期时间</param>
        /// <param name="func">要缓存对象</param>
        /// <returns></returns>
        public static HelperResult Cache(this HtmlHelper htmlHelper, string cacheKey, DateTime absoluteExpiration, TimeSpan slidingExpiration, Func<object, HelperResult> func)
        {
            return Cache(htmlHelper, cacheKey, null, absoluteExpiration, slidingExpiration, func);
        }

        /// <summary>
        /// 对页面中指定字符串进行缓存
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="cacheKey">缓存Key</param>
        /// <param name="absoluteExpiration">过期时间</param>
        /// <param name="func">要缓存对象</param>
        /// <returns></returns>
        public static HelperResult Cache(this HtmlHelper htmlHelper, string cacheKey, DateTime absoluteExpiration, Func<object, HelperResult> func)
        {
            return Cache(htmlHelper, cacheKey, null, absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, func);
        }
    }

呵呵,赶快去部署一下吧,有时,我们在考虑代码的功能性的同时,也应该多注意一下代码的调用的方便性与代码表现出现的艺术性!

感谢您的阅读!

感谢老赵同志!

posted @ 2013-01-24 11:50  张占岭  阅读(1288)  评论(1编辑  收藏  举报