Web API OData V4 Cache Part 9

This article shows how easy it is to implement cache with Web API OData. Web API OData V4 now supports Etag and also If-None-Match HTTP headers. These can then be accessed and set using the ODataQueryOptions inside an OData controller. This works well and could be a possible solution if you want to implement everything yourself. Another possible solution is CacheCow from Ali Kheyrollahi. This post is part 9 of the Web API OData V4 series.

Part 1 Getting started with Web API and OData V4 Part 1.
Part 2 Web API and OData V4 Queries, Functions and Attribute Routing Part 2
Part 3 Web API and OData V4 CRUD and Actions Part 3
Part 4 Web API OData V4 Using enum with Functions and Entities Part 4
Part 5 Web API OData V4 Using Unity IoC, SQLite with EF6 and OData Model Aliasing Part 5
Part 6 Web API OData V4 Using Contained Models Part 6
Part 7 Web API OData V4 Using a Singleton Part 7
Part 8 Web API OData V4 Using an OData T4 generated client Part 8
Part 9 Web API OData V4 Caching Part 9
Part 10 Web API OData V4 Batching Part 10
Part 11 Web API OData V4 Keys, Composite Keys and Functions Part 11

Code: https://github.com/damienbod/WebAPIODataV4SQLite

Using CacheCow with Web API Odata V4

This is very simple. Just add the CacheCow Server NuGet package to your server project.

Then add the CacheCow MessageHandler to the config:

var cacheCowCacheHandler = new CachingHandler(config);

Now all controllers including the OData controllers will use in memory cache from CacheCow.

You can test this with Fiddler. The first time you request a resource, you will recieve an Etag in the response header

ETag: W/”002a41972c3d43f0bb14d033907b3f41″

If you send this in your next request in the header, you will recieve a Http 304 which tells the client you already have the resource.

If-None-Match : W/”002a41972c3d43f0bb14d033907b3f41″


This all happens automatically in your browser. You can check this in Firefox with Firebug.

CacheCow also provides persistent cache solutions with different providers and also client caching solutions. Before you start implementing everything yourself with Etag and If-None-Match HTTP Headers, I would recommend looking at CacheCow first.








  1. Great article on caching with WebAPI OData! Brief introduction, easy to understand and all links one need! 🙂

  2. Reblogged this on Robin Sedlaczek's Blog and commented:
    Great article in WebAPI caching!

    1. Cheers Thanks
      greetings Damien

  3. Hi Damien,
    I’ve been using your Web API post series to implement my Web API OData 4 server. Your posts have proven invaluable! For thank I thank you many times!

    I just read part 9 of your series (Caching) and while I haven’t yet installed CacheCow in my project I’m intrigued by the following comment in CacheCow’s github page (https://github.com/aliostad/CacheCow):


    Currently CacheCow’s attribute setting does not work with Attribute Routing. And I personally think you should not use Attribute Routing…

    In part 2 of your series you mention that the way to access the different queries we have to use Attribute Routing (and actually only after correctly applying it I was able to start using my queries).

    Having said that, I ask you…what is your take on the note by CacheCow’s people? And what should happen if I have attribute routing and I install CacheCow?

    Thanks again!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: