KevinDockx / HttpCacheHeaders

ASP.NET Core middleware that adds HttpCache headers to responses (Cache-Control, Expires, ETag, Last-Modified), and implements cache expiration & validation models
MIT License
271 stars 57 forks source link

Provide the ability to ignore caching by default #109

Closed SeanFarrow closed 1 year ago

SeanFarrow commented 1 year ago

Hi,

I love this library.

I'm in a situation where I need to add caching for a specific endpoint in a larger application.

Currently, there is no caching in the rest of the application.

Is there a way to ignore caching by default and then just add it to the controller(s) I care about?

I could add it to the base controller, but want to know whether a nicer alternative exists, in case a controller is written that does not use the base controller, either now or in the future.

Thanks, Sean.

KevinDockx commented 1 year ago

Hi Sean,

there isn't at the moment, but I like the suggestion - I'll put it on the list of features to add.

KR, Kevin

SeanFarrow commented 1 year ago

Is it just as simple as on option somewhere (if yes where?) and then short-circuiting in the middleware if that option is set?

If this is the case I might have a crack at this in a few weeks.

Thanks, Sean.

From: Kevin Dockx @.> Sent: 23 January 2023 12:13 To: KevinDockx/HttpCacheHeaders @.> Cc: Sean Farrow @.>; Author @.> Subject: Re: [KevinDockx/HttpCacheHeaders] Provide the ability to ignore caching by default (Issue #109)

Hi Sean,

there isn't at the moment, but I like the suggestion - I'll put it on the list of features to add.

KR, Kevin

— Reply to this email directly, view it on GitHubhttps://github.com/KevinDockx/HttpCacheHeaders/issues/109#issuecomment-1400243479, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AALDK7UGLKX2UM3MNT3RVHDWTZYTBANCNFSM6AAAAAAUDNTYTM. You are receiving this because you authored the thread.Message ID: @.***>

SeanFarrow commented 1 year ago

@KevinDockx I have a branch with an implementation of this, would you like a PR?

DerStimmler commented 1 year ago

I'm really looking forward to this feature. I'm in the same situation that only several action methods need caching.

My current workaround without the use of a base controller is to set the default configuration for e.g. max-age to 0 when registering the service. That way the caching headers are still present but don't have any impact:

builder.Services.AddHttpCacheHeaders(
  expirationModelOptions =>
  {
    expirationModelOptions.MaxAge = 0;
    expirationModelOptions.NoStore = true;
    expirationModelOptions.NoTransform = true;
    expirationModelOptions.SharedMaxAge = 0;
    expirationModelOptions.CacheLocation = CacheLocation.Private;
  },
  validationModelOptions =>
  {
    validationModelOptions.NoCache = true;
  });

On action methods that need caching, I override this configuration with corresponding HttpCacheExpiration and HttpCacheValidation attributes.

An explicit feature to ignore caching by default would of course be a much better way.

Uli-Armbruster commented 1 year ago

I'm looking forward to see this feature implemented as well 👍

KevinDockx commented 1 year ago

@SeanFarrow : thanks for the PR, I accepted it! I've put some time aside next Tuesday to have a look in some more detail at this & a few other open issues in order to release a new version soon :)

KevinDockx commented 1 year ago

FYI, I had a look, fixed a small issue (HttpCacheExpiration/Valdation attributes should override the global setting & renamed the option: https://github.com/KevinDockx/HttpCacheHeaders/blob/master/src/Marvin.Cache.Headers/HttpCacheHeadersMiddleware.cs#L117. Next to that all is well, this will be in the next release - I'll release an preview in a few hours. Thanks again!