1

Why doesn't Web API come with caching features like MVC actions? Is it because these are HTTP based services so no state in between calls?

I have seen a few open sources like CacheCow and Strathweb, but not sure whom to pick and why? What are the best and standard options for caching with ASP.NET Web API?

Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
F11
  • 3,703
  • 12
  • 49
  • 83
  • If you are looking for HTTP Caching, have a look at (CacheCow)[https://github.com/aliostad/CacheCow]. – Aliostad Oct 08 '14 at 10:21

1 Answers1

2

This is an extensive article that explains the principal options, and contains links to many more information:

EXPLORING WEB API 2 CACHING

It includes information about:

The poor man's implementation consist in:

  • implement a cache store that supports storing and retrieving values by key
  • generate a key from the request properties, like action parameters, method, headers, and so on, to generate a key
  • check if a value for that key is available in the cache store:

    • if it is available, return it
    • if it isn't generate it, store it and return it

    var result = cacheStore.GetValue(keyFromRequest); if (result == null) { result = MyClass.ExpensiveFunctionCall(params); cacheStore.Store(keyFromRequest, result); } return result;

The cache store can be, for example, a database, a memory cache like MemoryCache class, or a Redis server.

The evolution of this idea is to use MVC action filters to make this cache cheking automatic, or to use a fully implemented solution like the aforementioned CacheCow

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Could you help me out in this problem http://stackoverflow.com/questions/26398129/caching-search-result-in-web-api – F11 Oct 16 '14 at 09:21
  • Will a database as a cache store not violate of definition of a cache which is there to prevent database queries at the first place! – user1451111 Aug 11 '18 at 00:18
  • 1
    @user1451111 No. A cache is something able to return a value in a faster or "cheaper" way that the usual method you'd use to get it. In your example , memory is faster than DB. But imagine you need to serve a request involving complicated calcultaions and queries to several external services. In that case getting the data from the DB is much faster than the original method to get it. – JotaBe Aug 13 '18 at 11:09