-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathInvalidateCacheOutputAttribute.cs
More file actions
41 lines (35 loc) · 1.56 KB
/
InvalidateCacheOutputAttribute.cs
File metadata and controls
41 lines (35 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Filters;
namespace WebApi.OutputCache.V2
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public sealed class InvalidateCacheOutputAttribute : BaseCacheAttribute
{
private string _controller;
private readonly string _methodName;
public InvalidateCacheOutputAttribute(string methodName)
: this(methodName, null)
{
}
public InvalidateCacheOutputAttribute(string methodName, Type type = null)
{
_controller = type != null ? type.FullName : null;
_methodName = methodName;
}
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
if (actionExecutedContext.Response != null && !actionExecutedContext.Response.IsSuccessStatusCode) return;
_controller = _controller ?? actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerType.FullName;
var config = actionExecutedContext.Request.GetConfiguration();
EnsureCache(config, actionExecutedContext.Request);
var key = actionExecutedContext.Request.GetConfiguration().CacheOutputConfiguration().MakeBaseCachekey(_controller, _methodName);
if (await WebApiCache.ContainsAsync(key))
{
await WebApiCache.RemoveStartsWithAsync(key);
}
}
}
}