Return result is IStatusCodeActionResult statusCodeActionResult & statusCodeActionResult.StatusCode is >= 200 and = 200 and < 300).Īlso, no need for the app.UseResponseCaching() middleware in either case, as it doesn't control the Cache-Control headers it just reads them (as might be set by the ResponseCache attribute), and caches cacheable responses to implement server-side caching. Private bool ResultIsSuccess(IActionResult result) Public override void OnActionExecuted(ActionExecutedContext context) Instead, I wrote my own action filter: public class CacheControlAttribute : ActionFilterAttribute The ResponseCacheFilter ( source) is the action filter that actually sets the Cache-Control headers, but it doesn't take into account status codes, so it's not actually possible to do what I wanted. How can I stop error responses (or any non-200 response) from being cached? Any other responses, including error pages, are ignored by the middleware. Response Caching Middleware only caches server responses that result in a 200 (OK) status code. When I hit the /fail endpoint, it returns a 400 status as expected but it has a Cache-Control header of public,max-age=60 because of the ResponseCache attribute on the action method. Public class TestController : ControllerBase Public void Configure(IApplicationBuilder app, IWebHostEnvironment env) SetSlidingExpiration (TimeSpan.Public void ConfigureServices(IServiceCollection services) 检查用户名是否存在 1 // Check if the username exists public JsonResult SelectUName( string uname) 2 public JsonResult SelectUName ( string uname) Before using IMemor圜ache you need to add a reference, and execute the command in the package management console to install the dependent package: In-memory and distributed caches store cached items as key / value pairs. ![]() Keep in mind that we’re talking about caching only the GET responses and not for any other Request Methods, since other method requests such as POST or PUT or others can contain data which might change frequently or is not suggested to be cached. The in-memory cache can store any object the distributed cache interface is limited to byte. ASP.NET Core framework provides a middleware to handle response caching at a single place once the response has been generated and just before it is passed onto the user. Use distributed cache to offload cache memory to external processes. ![]() For some applications, distributed caching can support higher out-scaling than in-memory caching. Non-sticky sessions in a web farm require distributed caching to avoid cache coherency issues. For example, Azure Web Apps use Application Request Routing (ARR) to route all subsequent requests to the same server. Sticky sessions ensure that subsequent requests from clients go to the same server. Applications running on multiple servers in a server farm should ensure that sessions are stuck when using in-memory caching. The simplest cache is based on IMemor圜ache, which represents a cache stored in the web server's memory. Applications should be written and tested to never rely on cached data.ĪSP.NET Core supports many different caches. Cache-generated copies of data can be returned faster than from the original source. ![]() Caching works best for data that changes infrequently. ![]() Using Cache in ASP.NET Core Cache introduction:īy reducing the work required to generate content, caching can significantly improve application performance and scalability.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |