Output Caching in ASP.NET Web API 2 - StrathWeb

Strath

November 7th, 2013

Output Caching in ASP.NET Web API 2

A while ago I released a little library for Web API caching – called CacheOutput.

With the release of Web API 2, some things change (obviously the Web API core gets a bump to 5.0) so the library required a new release. I have decided to take a page out of the book of my friends over at Autofac, and do not release over the old Nuget package, but instead release a new one specifically for Web API 2.

If you are in need of caching for Web API 2, you should use the package called Strathweb.CacheOutput.WebApi2

Strathweb.CacheOutput.WebApi2

It’s available on Nuget since 2 weeks ago, but I forgot to blog about it :)

So, for Web API 2 (.NET 4.5) use:

For Web API 1 (.NET 4.0), you can still use the old one:

Main reason to do it like that is to avoid the hassle of binding redirects and all that. Also, a lot of people still use Web API 1 so I think it’s clean like this – I really like this approacch in i.e. Autofac.

All the documentation over at Github is still valid, and both versions are hosted there.

Be Sociable, Share!

  • Pingback: Dew Drop – November 7, 2013 (#1662) | Morning Dew

  • alpriest

    Is there a way of controlling the cache durations from within the action rather than decorating with attributes? My action can return data with varying degrees of cacheability so one cache duration doesn’t give me the granularity i’d like.

  • Pingback: End of Month Research Roundup – January 2014 | endjin blog

  • Jonatas Saraiva

    Works with Azure cache?

  • Devesh

    Is there anyway i can retrieve from Cache using key?I have a Get method and search method in web api and i am caching Get Method now in search method i want to search from the same Cache.

    ///
    /// Returns a movie
    ///
    /// movieId
    /// Movie
    [CacheOutput(ClientTimeSpan = 86400, ServerTimeSpan = 86400)]
    public Movie Get(int movieId)
    {
    var movie = repository.GetMovieById(movieId);
    if (movie == null)
    {
    var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.NotFound)
    {
    Content = new StringContent(string.Format(“No movie with ID = {0}”, movieId)),
    ReasonPhrase = “Movie ID Not Found”
    };
    throw new HttpResponseException(httpResponseMessage);
    }
    return movie;
    }

    ///

    /// Search across all fields within the movie

    ///

    /// Search Parameter

    /// Movies

    [ActionName("Search")]

    public IEnumerable GetMovieBySearchParameter(string searchstr)

    {

    IEnumerable Searchresults = null;

    if (repository != null)

    {

    if (cache.Get(“GetResult”) != null)

    {

    var Result = cache.Get(“GetResult”) as IEnumerable;

    var query =

    from movie in Result

    where

    (movie.Title != null && movie.Title.Contains(searchstr)) ||

    (movie.Genre != null && movie.Genre.Contains(searchstr)) ||

    (movie.Classification != null && movie.Classification.Contains(searchstr)) ||

    (movie.Cast != null && movie.Cast.Contains(searchstr)) ||

    (movie.Rating.ToString() != null && movie.Rating.ToString().Contains(searchstr)) ||

    (movie.ReleaseYear.ToString() != null && movie.ReleaseYear.ToString().Contains(searchstr))

    select movie;

    Searchresults = query.AsEnumerable().OrderBy(c => c.MovieId);

    }

    else

    {

    var query =

    from movie in repository.GetMovies()

    where

    (movie.Title != null && movie.Title.Contains(searchstr)) ||

    (movie.Genre != null && movie.Genre.Contains(searchstr)) ||

    (movie.Classification != null && movie.Classification.Contains(searchstr)) ||

    (movie.Cast != null && movie.Cast.Contains(searchstr)) ||

    (movie.Rating.ToString() != null && movie.Rating.ToString().Contains(searchstr)) ||

    (movie.ReleaseYear.ToString() != null && movie.ReleaseYear.ToString().Contains(searchstr))

    select movie;

    Searchresults= query.AsEnumerable().OrderBy(c => c.MovieId);

    cache.Add(“GetResult”, Searchresults, policy);

    }

    return Searchresults;

    }

    else

    {

    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));

    }

    }

    In the above code i am caching the same thing in Get and Search,Can i store using key?