11using System ;
22using System . Globalization ;
33using System . IO ;
4+ using System . Web ;
45using System . Web . Mvc ;
56using System . Web . UI ;
6- using System . Web ;
77
88namespace DevTrends . MvcDonutCaching
99{
1010 [ AttributeUsage ( AttributeTargets . Class | AttributeTargets . Method , Inherited = true , AllowMultiple = false ) ]
1111 public class DonutOutputCacheAttribute : ActionFilterAttribute , IExceptionFilter
1212 {
13- private const string CallbackKey = "d0nutCallback" ;
14-
1513 private readonly IKeyGenerator _keyGenerator ;
1614 private readonly IDonutHoleFiller _donutHoleFiller ;
1715 private readonly IExtendedOutputCacheManager _outputCacheManager ;
1816 private readonly ICacheSettingsManager _cacheSettingsManager ;
1917 private readonly ICacheHeadersHelper _cacheHeadersHelper ;
2018
2119 private CacheSettings _cacheSettings ;
22- private string _cacheKey ;
2320
2421 public int Duration { get ; set ; }
2522 public string VaryByParam { get ; set ; }
@@ -45,11 +42,11 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
4542 {
4643 _cacheSettings = BuildCacheSettings ( ) ;
4744
45+ var cacheKey = _keyGenerator . GenerateKey ( filterContext , _cacheSettings ) ;
46+
4847 if ( _cacheSettings . IsServerCachingEnabled )
4948 {
50- _cacheKey = _keyGenerator . GenerateKey ( filterContext , _cacheSettings ) ;
51-
52- var cachedItem = _outputCacheManager . GetItem ( _cacheKey ) ;
49+ var cachedItem = _outputCacheManager . GetItem ( cacheKey ) ;
5350
5451 if ( cachedItem != null )
5552 {
@@ -63,17 +60,15 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
6360
6461 if ( filterContext . Result == null )
6562 {
66- var callbackKey = BuildCallbackKey ( filterContext ) ;
67-
6863 var cachingWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
6964
7065 var originalWriter = filterContext . HttpContext . Response . Output ;
7166
7267 filterContext . HttpContext . Response . Output = cachingWriter ;
7368
74- filterContext . HttpContext . Items [ callbackKey ] = new Action < bool > ( hasErrors =>
69+ filterContext . HttpContext . Items [ cacheKey ] = new Action < bool > ( hasErrors =>
7570 {
76- filterContext . HttpContext . Items . Remove ( callbackKey ) ;
71+ filterContext . HttpContext . Items . Remove ( cacheKey ) ;
7772
7873 filterContext . HttpContext . Response . Output = originalWriter ;
7974
@@ -89,7 +84,7 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
8984
9085 if ( _cacheSettings . IsServerCachingEnabled && filterContext . HttpContext . Response . StatusCode == 200 )
9186 {
92- _outputCacheManager . AddItem ( _cacheKey , cacheItem , DateTime . Now . AddSeconds ( _cacheSettings . Duration ) ) ;
87+ _outputCacheManager . AddItem ( cacheKey , cacheItem , DateTime . Now . AddSeconds ( _cacheSettings . Duration ) ) ;
9388 }
9489 }
9590 } ) ;
@@ -113,24 +108,16 @@ public void OnException(ExceptionContext filterContext)
113108
114109 private void ExecuteCallback ( ControllerContext context , bool hasErrors )
115110 {
116- var callbackKey = BuildCallbackKey ( context ) ;
111+ var cacheKey = _keyGenerator . GenerateKey ( context , _cacheSettings ) ;
117112
118- var callback = context . HttpContext . Items [ callbackKey ] as Action < bool > ;
113+ var callback = context . HttpContext . Items [ cacheKey ] as Action < bool > ;
119114
120115 if ( callback != null )
121116 {
122117 callback . Invoke ( hasErrors ) ;
123118 }
124119 }
125120
126- private string BuildCallbackKey ( ControllerContext context )
127- {
128- var actionName = context . RouteData . Values [ "action" ] . ToString ( ) ;
129- var controllerName = context . RouteData . Values [ "controller" ] . ToString ( ) ;
130-
131- return string . Format ( "{0}.{1}.{2}" , CallbackKey , controllerName , actionName ) ;
132- }
133-
134121 private CacheSettings BuildCacheSettings ( )
135122 {
136123 CacheSettings cacheSettings ;
0 commit comments