/// <summary> /// View页面片断缓存 /// </summary> /// <param name="html"></param> /// <param name="cacheKey">缓存键</param> /// <param name="cacheDependency">缓存依赖项</param> /// <param name="absoluteExpiration">绝对过期时间</param> /// <param name="slidingExpiration">滑动过期时间</param> /// <param name="action">缓存无效时执行的页面内容片断</param> public static void Cache(this HtmlHelper html, string cacheKey, CacheDependency cacheDependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, Action action) { Cache cache = html.ViewContext.HttpContext.Cache; string content = cache.Get(cacheKey) as string; if (content == null) { RecordWriter writer = html.GetRecordWriter(); StringBuilder recorder = new StringBuilder(); writer.AddRecorder(recorder); action(); writer.RemoveRecorder(recorder); content = recorder.ToString(); cache.Insert(cacheKey, content, cacheDependency, absoluteExpiration, slidingExpiration); } html.ViewContext.Writer.Write(content); }