Skip to content

Commit 84b8bcd

Browse files
committed
added IgnoreCacheOutputAttribute
1 parent 42232de commit 84b8bcd

7 files changed

Lines changed: 52 additions & 8 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Web.Http;
3+
4+
namespace WebApi.OutputCache.V2.Demo
5+
{
6+
[CacheOutput(ClientTimeSpan = 50, ServerTimeSpan = 50)]
7+
[IgnoreCacheOutput]
8+
[RoutePrefix("ignore")]
9+
public class IgnoreController : ApiController
10+
{
11+
[Route("cached")]
12+
public string GetCached()
13+
{
14+
return DateTime.Now.ToString();
15+
}
16+
17+
[Route("uncached")]
18+
public string GetUnCached()
19+
{
20+
return DateTime.Now.ToString();
21+
}
22+
}
23+
}

sample/WebApi.OutputCache.V2.Demo/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Program
1010
static void Main(string[] args)
1111
{
1212
var config = new HttpSelfHostConfiguration("http://localhost:999");
13+
config.MapHttpAttributeRoutes();
1314
config.Routes.MapHttpRoute(
1415
name: "DefaultApi",
1516
routeTemplate: "api/{controller}/{id}",

sample/WebApi.OutputCache.V2.Demo/TeamsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public IEnumerable<Team> Get()
2121
return Teams;
2222
}
2323

24-
[CacheOutputUntil(2014, 7, 20)]
24+
[CacheOutputUntil(2016, 7, 20)]
2525
public Team GetById(int id)
2626
{
2727
var team = Teams.FirstOrDefault(i => i.Id == id);

sample/WebApi.OutputCache.V2.Demo/WebApi.OutputCache.V2.Demo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Reference Include="System.Xml" />
6666
</ItemGroup>
6767
<ItemGroup>
68+
<Compile Include="IgnoreController.cs" />
6869
<Compile Include="Teams2Controller.cs" />
6970
<Compile Include="Program.cs" />
7071
<Compile Include="Team.cs" />

src/WebApi.OutputCache.V2/CacheOutputAttribute.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace WebApi.OutputCache.V2
1919
{
20-
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
20+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
2121
public class CacheOutputAttribute : ActionFilterAttribute
2222
{
2323
private const string CurrentRequestMediaType = "CacheOutput:CurrentRequestMediaType";
@@ -73,14 +73,23 @@ protected virtual void EnsureCache(HttpConfiguration config, HttpRequestMessage
7373

7474
internal IModelQuery<DateTime, CacheTime> CacheTimeQuery;
7575

76-
readonly Func<HttpActionContext, bool, bool> _isCachingAllowed = (ac, anonymous) =>
76+
protected virtual bool IsCachingAllowed(HttpActionContext actionContext, bool anonymousOnly)
7777
{
78-
if (anonymous)
78+
if (anonymousOnly)
79+
{
7980
if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
81+
{
8082
return false;
83+
}
84+
}
8185

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+
}
8493

8594
protected virtual void EnsureCacheTimeQuery()
8695
{
@@ -133,7 +142,7 @@ public override void OnActionExecuting(HttpActionContext actionContext)
133142
{
134143
if (actionContext == null) throw new ArgumentNullException("actionContext");
135144

136-
if (!_isCachingAllowed(actionContext, AnonymousOnly)) return;
145+
if (!IsCachingAllowed(actionContext, AnonymousOnly)) return;
137146

138147
var config = actionContext.Request.GetConfiguration();
139148

@@ -184,7 +193,7 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio
184193
{
185194
if (actionExecutedContext.ActionContext.Response == null || !actionExecutedContext.ActionContext.Response.IsSuccessStatusCode) return;
186195

187-
if (!_isCachingAllowed(actionExecutedContext.ActionContext, AnonymousOnly)) return;
196+
if (!IsCachingAllowed(actionExecutedContext.ActionContext, AnonymousOnly)) return;
188197

189198
var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
190199
if (cacheTime.AbsoluteExpiration > DateTime.Now)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace WebApi.OutputCache.V2
4+
{
5+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
6+
public sealed class IgnoreCacheOutputAttribute : Attribute
7+
{
8+
}
9+
}

src/WebApi.OutputCache.V2/WebApi.OutputCache.V2.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="DefaultCacheKeyGenerator.cs" />
6262
<Compile Include="HttpConfigurationExtensions.cs" />
6363
<Compile Include="ICacheKeyGenerator.cs" />
64+
<Compile Include="IgnoreCacheOutputAttribute.cs" />
6465
<Compile Include="InvalidateCacheOutputAttribute.cs" />
6566
<Compile Include="Properties\AssemblyInfo.cs" />
6667
<Compile Include="CacheOutputAttribute.cs" />

0 commit comments

Comments
 (0)