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
Using CacheCow with Web API Odata V4
Then add the CacheCow MessageHandler to the config:
var cacheCowCacheHandler = new CachingHandler(config); config.MessageHandlers.Add(cacheCowCacheHandler);
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
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″
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.