|
17 | 17 |
|
18 | 18 | namespace WebApi.OutputCache.V2 |
19 | 19 | { |
20 | | - [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] |
| 20 | + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)] |
21 | 21 | public class CacheOutputAttribute : ActionFilterAttribute |
22 | 22 | { |
23 | 23 | private const string CurrentRequestMediaType = "CacheOutput:CurrentRequestMediaType"; |
@@ -73,14 +73,23 @@ protected virtual void EnsureCache(HttpConfiguration config, HttpRequestMessage |
73 | 73 |
|
74 | 74 | internal IModelQuery<DateTime, CacheTime> CacheTimeQuery; |
75 | 75 |
|
76 | | - readonly Func<HttpActionContext, bool, bool> _isCachingAllowed = (ac, anonymous) => |
| 76 | + protected virtual bool IsCachingAllowed(HttpActionContext actionContext, bool anonymousOnly) |
77 | 77 | { |
78 | | - if (anonymous) |
| 78 | + if (anonymousOnly) |
| 79 | + { |
79 | 80 | if (Thread.CurrentPrincipal.Identity.IsAuthenticated) |
| 81 | + { |
80 | 82 | return false; |
| 83 | + } |
| 84 | + } |
81 | 85 |
|
82 | | - return ac.Request.Method == HttpMethod.Get; |
83 | | - }; |
| 86 | + if (actionContext.ActionDescriptor.GetCustomAttributes<IgnoreCacheOutputAttribute>().Any()) |
| 87 | + { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + return actionContext.Request.Method == HttpMethod.Get; |
| 92 | + } |
84 | 93 |
|
85 | 94 | protected virtual void EnsureCacheTimeQuery() |
86 | 95 | { |
@@ -133,7 +142,7 @@ public override void OnActionExecuting(HttpActionContext actionContext) |
133 | 142 | { |
134 | 143 | if (actionContext == null) throw new ArgumentNullException("actionContext"); |
135 | 144 |
|
136 | | - if (!_isCachingAllowed(actionContext, AnonymousOnly)) return; |
| 145 | + if (!IsCachingAllowed(actionContext, AnonymousOnly)) return; |
137 | 146 |
|
138 | 147 | var config = actionContext.Request.GetConfiguration(); |
139 | 148 |
|
@@ -184,7 +193,7 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio |
184 | 193 | { |
185 | 194 | if (actionExecutedContext.ActionContext.Response == null || !actionExecutedContext.ActionContext.Response.IsSuccessStatusCode) return; |
186 | 195 |
|
187 | | - if (!_isCachingAllowed(actionExecutedContext.ActionContext, AnonymousOnly)) return; |
| 196 | + if (!IsCachingAllowed(actionExecutedContext.ActionContext, AnonymousOnly)) return; |
188 | 197 |
|
189 | 198 | var cacheTime = CacheTimeQuery.Execute(DateTime.Now); |
190 | 199 | if (cacheTime.AbsoluteExpiration > DateTime.Now) |
|
0 commit comments