forked from filipw/Strathweb.CacheOutput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebApiOutputCacheAttribute.cs
112 lines (94 loc) · 4.19 KB
/
WebApiOutputCacheAttribute.cs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Filters;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Net.Http.Headers;
using System.Runtime.Caching;
using System.Threading;
namespace WebApi.OutputCache
{
public class WebApiOutputCacheAttribute : ActionFilterAttribute
{
// cache length in seconds
private int _timespan = 0;
// client cache length in seconds
private int _clientTimeSpan = 0;
// cache for anonymous users only?
private bool _anonymousOnly = false;
// cache key
private string _cachekey = string.Empty;
// cache repository
private static readonly ObjectCache WebApiCache = MemoryCache.Default;
Func<int, HttpActionContext, bool, bool> _isCachingTimeValid = (timespan, ac, anonymous) =>
{
if (timespan > 0)
{
if (anonymous)
if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
return false;
if (ac.Request.Method == HttpMethod.Get) return true;
}
return false;
};
private CacheControlHeaderValue setClientCache()
{
var cachecontrol = new CacheControlHeaderValue();
cachecontrol.MaxAge = TimeSpan.FromSeconds(_clientTimeSpan);
cachecontrol.MustRevalidate = true;
return cachecontrol;
}
public WebApiOutputCacheAttribute(int timespan, int clientTimeSpan, bool anonymousOnly)
{
_timespan = timespan;
_clientTimeSpan = clientTimeSpan;
_anonymousOnly = anonymousOnly;
}
public override void OnActionExecuting(HttpActionContext ac)
{
if (ac != null)
{
if (_isCachingTimeValid(_timespan, ac, _anonymousOnly))
{
MediaTypeWithQualityHeaderValue mediaTypeWithQualityHeaderValue = ac.Request.Headers.Accept.FirstOrDefault();
string acceptHeader = (mediaTypeWithQualityHeaderValue == null)
? "Default"
: mediaTypeWithQualityHeaderValue.ToString();
_cachekey = string.Join(":", new string[] { ac.Request.RequestUri.PathAndQuery, acceptHeader });
if (WebApiCache.Contains(_cachekey))
{
var val = WebApiCache.Get(_cachekey) as string;
if (val != null)
{
var contenttype = (MediaTypeHeaderValue)WebApiCache.Get(_cachekey + ":response-ct");
if (contenttype == null)
contenttype = new MediaTypeHeaderValue(_cachekey.Split(':')[1]);
ac.Response = ac.Request.CreateResponse();
ac.Response.Content = new StringContent(val);
ac.Response.Content.Headers.ContentType = contenttype;
ac.Response.Headers.CacheControl = this.setClientCache();
return;
}
}
}
}
else
{
throw new ArgumentNullException("actionContext");
}
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (!(WebApiCache.Contains(_cachekey)) && !string.IsNullOrWhiteSpace(_cachekey))
{
var body = actionExecutedContext.Response.Content.ReadAsStringAsync().Result;
WebApiCache.Add(_cachekey, body, DateTime.Now.AddSeconds(_timespan));
WebApiCache.Add(_cachekey + ":response-ct", actionExecutedContext.Response.Content.Headers.ContentType, DateTime.Now.AddSeconds(_timespan));
}
if (_isCachingTimeValid(_clientTimeSpan, actionExecutedContext.ActionContext, _anonymousOnly))
actionExecutedContext.ActionContext.Response.Headers.CacheControl = setClientCache();
}
}
}