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?

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Could you help me out in this problem stackoverflow.com/questions/26398129/…
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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.