In its current form the Web Api doesn't seem to understand the OutputCache-attribute. But is there some examples or best practices on how the responses could be cached? Taking the simples case where a Get-operation doesn't have any parameters, how could
the response be cached for 5 minutes?
Unfortunately I don't agree with this. Given an API which returns something simple like the latest stock quote for MSFT, I of course would like to use the output cache to store the response. The API can be accessed from millions of client and it doesn't
help if one client caches the reply.
In its current form the Web Api doesn't seem to understand the OutputCache-attribute. But is there some examples or best practices on how the responses could be cached? Taking the simples case where a Get-operation doesn't have any parameters, how could
the response be cached for 5 minutes?
One could always create a custom ActionFilterAttribute or DelegatingHandler that would handle the caching for you. One problem I foresee is if you rely on the ASP.NET caching, it won't be available if you are self hosting.
For example ActionFilterAttribute.OnActionExecuting you would check the cache & return the response. ActionFilterAttribute.OnActionExecuted, you would stuff the results into the cache. Another problem you would need to be mindful of is OnActionExecuted receiving
a response message with streaming content...
Just a quickdemo which works in both host(tested in self host). Note, this just a
demo but you can easily improve this by spending some time on this,
public class OutputCacheAttribute : ActionFilterAttribute
{
int _duration;
string _key;
public OutputCacheAttribute(int duration, string key)
{
if (duration <= 0)
throw new InvalidOperationException("Invalid Duration");
if (string.IsNullOrWhiteSpace(key))
throw new InvalidOperationException("Invalid Key");
_key = key;
_duration = duration;
}
private static ObjectCache Cache
{
get
{
return MemoryCache.Default;
}
}
private Action<HttpActionExecutedContext> Callback { set; get; }
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext == null)
{
throw new ArgumentNullException("actionExecutedContext");
}
string cachedValue = Cache.Get(_key) as string;
if (cachedValue != null)
{
actionContext.Response = actionContext.Request.CreateResponse();
actionContext.Response.Content = new StringContent(cachedValue);
actionContext.Response.Content.Headers.ContentType = new MediaTypeHeaderValue(Cache.Get(_key + "+ContentType").ToString());
return;
}
Callback = (actionExecutedContext) =>
{
var output = actionExecutedContext.Result.Content.ReadAsStringAsync().Result;
Cache.Add(_key, output, DateTimeOffset.UtcNow.AddSeconds(_duration));
Cache.Add(_key + "+ContentType", actionExecutedContext.Result.Content.Headers.ContentType.MediaType, DateTimeOffset.UtcNow.AddSeconds(_duration));
};
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext == null)
{
throw new ArgumentNullException("actionExecutedContext");
}
Callback(actionExecutedContext);
}
}
For improving this you must understand how Web API works. Also, digging into MVC 3 OutputCacheAttribute class will also improve this.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
mikoskinen
0 Points
3 Posts
The best way to handle output caching in Web Api?
Apr 14, 2012 10:08 AM|LINK
In its current form the Web Api doesn't seem to understand the OutputCache-attribute. But is there some examples or best practices on how the responses could be cached? Taking the simples case where a Get-operation doesn't have any parameters, how could the response be cached for 5 minutes?
hiza808
Member
270 Points
75 Posts
Re: The best way to handle output caching in Web Api?
Apr 15, 2012 07:51 PM|LINK
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/improving-performance-with-output-caching-cs
mikoskinen
0 Points
3 Posts
Re: The best way to handle output caching in Web Api?
Apr 16, 2012 04:25 AM|LINK
Thank you for the link. Unfortunately that article only seems to show how to use the OutputCache-attribute, which is not supported in Web Api.
digitalpacma...
Member
183 Points
148 Posts
Re: The best way to handle output caching in Web Api?
Apr 16, 2012 04:33 PM|LINK
Since you are doing REST, you are suppposed to rely on HTTP caching. Meaning the clients will cache your response. It's not really the servers job.
mikoskinen
0 Points
3 Posts
Re: The best way to handle output caching in Web Api?
Apr 18, 2012 06:47 AM|LINK
Thank for the message.
Unfortunately I don't agree with this. Given an API which returns something simple like the latest stock quote for MSFT, I of course would like to use the output cache to store the response. The API can be accessed from millions of client and it doesn't help if one client caches the reply.
Jay_Harlow
Member
86 Points
26 Posts
Re: The best way to handle output caching in Web Api?
Apr 18, 2012 12:56 PM|LINK
One could always create a custom ActionFilterAttribute or DelegatingHandler that would handle the caching for you. One problem I foresee is if you rely on the ASP.NET caching, it won't be available if you are self hosting.
For example ActionFilterAttribute.OnActionExecuting you would check the cache & return the response. ActionFilterAttribute.OnActionExecuted, you would stuff the results into the cache. Another problem you would need to be mindful of is OnActionExecuted receiving a response message with streaming content...
Hope this helps
Jay
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: The best way to handle output caching in Web Api?
Apr 18, 2012 05:46 PM|LINK
Just a quick demo which works in both host(tested in self host). Note, this just a demo but you can easily improve this by spending some time on this,
public class OutputCacheAttribute : ActionFilterAttribute { int _duration; string _key; public OutputCacheAttribute(int duration, string key) { if (duration <= 0) throw new InvalidOperationException("Invalid Duration"); if (string.IsNullOrWhiteSpace(key)) throw new InvalidOperationException("Invalid Key"); _key = key; _duration = duration; } private static ObjectCache Cache { get { return MemoryCache.Default; } } private Action<HttpActionExecutedContext> Callback { set; get; } public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext == null) { throw new ArgumentNullException("actionExecutedContext"); } string cachedValue = Cache.Get(_key) as string; if (cachedValue != null) { actionContext.Response = actionContext.Request.CreateResponse(); actionContext.Response.Content = new StringContent(cachedValue); actionContext.Response.Content.Headers.ContentType = new MediaTypeHeaderValue(Cache.Get(_key + "+ContentType").ToString()); return; } Callback = (actionExecutedContext) => { var output = actionExecutedContext.Result.Content.ReadAsStringAsync().Result; Cache.Add(_key, output, DateTimeOffset.UtcNow.AddSeconds(_duration)); Cache.Add(_key + "+ContentType", actionExecutedContext.Result.Content.Headers.ContentType.MediaType, DateTimeOffset.UtcNow.AddSeconds(_duration)); }; } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { if (actionExecutedContext == null) { throw new ArgumentNullException("actionExecutedContext"); } Callback(actionExecutedContext); } }For improving this you must understand how Web API works. Also, digging into MVC 3 OutputCacheAttribute class will also improve this.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Tohid
Member
11 Points
6 Posts
Re: The best way to handle output caching in Web Api?
Dec 20, 2012 07:38 PM|LINK
Your answer is here:
http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/