![]() ![]() The Content-Length header value (if set) must match the size of the response body.Vary header parameters must be valid and not equal to *.The Set-Cookie header must not be present.The Pragma: no-cache header must not be present if the Cache-Control header isn’t present, as the Cache-Control header overrides the Pragma header when present.Cache-Control header parameters must be valid, and the response must be marked public and not marked private.The Authorization header must not be present.For more information, see ASP.NET Core Middleware. In Startup.Configure, Response Caching Middleware must be placed before middleware that require caching.The request method must be GET or HEAD.The request must result in a server response with a 200 (OK) status code.Our caching middleware is ready to be used, but we need to define on what endpoint it has to be activated, because for now it’s activated on any endpoint in within the app and manage siteId parameter in every endpoints within the app as well.Īccording to Microsoft documentation here are conditions of caching: Note that how its easy to access to the configuration from options we defined juste before (ConfigurationCachingOptions). Here is the implementation of the middleware: This feature is activated when you configure your application with services.AddResponseCaching() and app.UseResponseCaching() in your Startup.cs. The caching middleware has to be able to manage query string parameters (siteId) so we need to implement IResponseCachingFeature. The value “Location” is set to “Client” because I want to cache it client only, then “Cache-Control” header will be set to “Private”, “Public” if I choose “Any” if I want to set it Client side or in any Proxy. Define caching location (in my case client side). ![]() Let’s start by configuring option values, in my demo I will need two options: The third scenario is different from the first two, because the route parameter, there are as many urls as possible siteId while others are the same url but only the query parameter changes. If we take a look of all possible urls to manage, we have Expose an endpoint that returns a configuration for a specified route filter.Expose an endpoint that takes optionally a query string parameter (a method that retrieves all my configuration + the possibility to filter it).In this article I wanted to explain to you how I did it. So I had the idea of building a caching middleware, conditional on the route used, in order to have the same behavior as an action or controller attribute. But something bothered me, I wanted to have the possibility of benefiting from a greater flexibility in parameters management, for example by using dependency injection to inject parameters rather than using caching profiles. So I only had as solution the HTTP response cache by attributes because client side caching. My goal was to avoid making HTTP hits to the server, so I had to eliminate the distributed cache and the memory cache. It was not that long ago that I had to manage the cache to get informations that rarely change. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |