Click here to Skip to main content
15,881,588 members
Articles / Web Development / ASP.NET
Tip/Trick

Implement Caching in Web API

Rate me:
Please Sign up or sign in to vote.
4.88/5 (7 votes)
14 Oct 2014CPOL 46K   22   2
Output caching does not work for ASP.NET Web API. This tip is a solution to implement caching in web API.

Problem in Output Caching

If ASP.NET "OutputCache" added to rest method, "cache-control" in "Http Response Headers" always shows "no-cache".

Example

C#
public class ProductController : ApiController
{
    [OutputCache(Duration = 120)]
    public string Get(int id)
    {
        return "Product"+ 1;
    }
}

Response in Chrome extension - XHR-Poster

Image 1

Solution

It's easy to implement basic caching using "ActionFilterAttribute". We need to add cache control in OnActionExecuted methods as follows:

C#
public class CacheClientAttribute : ActionFilterAttribute
{
    public int Duration { get; set; }
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        actionExecutedContext.Response.Headers.CacheControl = new CacheControlHeaderValue
        {
            MaxAge = TimeSpan.FromSeconds(Duration),
            MustRevalidate = true,
            Public = true
        }
    }
}

Now, we can use this attribute in API method.

C#
public class ProductController : ApiController
{
     [CacheClient(Duration = 120)]
     public string Get(int id)
     {
         return "Product"+ 1;
     }
}

Response in Chrome extension - XHR-Poster

Image 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI think you're confusing server-side caching with and client-side caching Pin
Peter Josefsson Sweden15-Oct-14 2:13
Peter Josefsson Sweden15-Oct-14 2:13 
Hi,

Note that "output caching" is purely server-side - it means that the SERVER should cache the result locally and send the cached result to clients until it expires. This is transparent to the client and can't be seen in the headers (it has nothing to do with any caching the client does - it's only there to save server processing time).

So, it may very well be (I haven't investigated) that the server-side caching in your example works. You could test that by generating a different result on each request (without the client-side caching enabled) and see what you get. If you get the same result twice in succession (with client-side caching turned OFF), it works.

However, your code for enabling client-side caching is OK, and a really nice (clear and simple) example of doing this kind of stuff with an attribute. An excercise for the reader would be to implement ETAGs, Last-Modified-When and so on, but that's maybe a bit extracurricular.

/Peter
Peter the small turnip

(1) It Has To Work. --RFC 1925[^]

AnswerOutputCache attribute Pin
Mario Majčica15-Oct-14 2:12
professionalMario Majčica15-Oct-14 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.