Go to the documentation of this file.00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Web.Mvc;
00006 using System.Web.UI;
00007 using System.Web;
00008 using N2.Web.UI;
00009 using System.Security.Principal;
00010 using System.Web.Routing;
00011
00012 namespace N2.Web.Mvc
00013 {
00019 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
00020 public class ContentOutputCacheAttribute : ActionFilterAttribute
00021 {
00022 public override void OnResultExecuting(ResultExecutingContext filterContext)
00023 {
00024 if (filterContext == null)
00025 throw new ArgumentNullException("filterContext");
00026
00027 if (filterContext.IsChildAction)
00028 return;
00029
00030 ProcessOutputCache(filterContext.RequestContext);
00031 }
00032
00033 private static void ProcessOutputCache(RequestContext requestContext)
00034 {
00035 if (HttpContext.Current == null)
00036 return;
00037
00038 var user = requestContext.HttpContext.User;
00039 if (user == null || user.Identity.IsAuthenticated)
00040 return;
00041
00042 ICacheManager cacheManager = requestContext.RouteData.ResolveService<ICacheManager>();
00043 if (!cacheManager.Enabled)
00044 return;
00045
00046 var page = new OutputCachedPage(cacheManager.GetOutputCacheParameters());
00047 page.ProcessRequest(HttpContext.Current);
00048 cacheManager.AddCacheInvalidation(HttpContext.Current.Response);
00049 }
00050
00051 private sealed class OutputCachedPage : Page
00052 {
00053 private OutputCacheParameters _cacheSettings;
00054
00055 public OutputCachedPage(OutputCacheParameters cacheSettings)
00056 {
00057 this.ID = Guid.NewGuid().ToString();
00058 this._cacheSettings = cacheSettings;
00059 }
00060
00061 protected override void FrameworkInitialize()
00062 {
00063 base.FrameworkInitialize();
00064 this.InitOutputCache(this._cacheSettings);
00065 }
00066 }
00067 }
00068 }