Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Cache #244

Closed
circleupx opened this issue Jan 13, 2020 · 8 comments
Closed

Disable Cache #244

circleupx opened this issue Jan 13, 2020 · 8 comments

Comments

@circleupx
Copy link

Hi,

I have a question, is there a way to disable CacheCow?

I would like to disable the cache whenever I am working on my local environment, my current workaround is to force chrome to disable caching.

Is there a better way to do this? Also, this is an awesome project!

@aliostad
Copy link
Owner

Hi,

Could you please explain more? Do you mean you have a need for configuration based on the environments?

But then again, in your client, you can send no-cache header so that the server always send you the fresh resource.

@circleupx
Copy link
Author

Hi,

Yes, I guess that is what I am looking for, but just for my local environment.

I've been doing what you are suggesting, basically, in postman/chrome I sent the appropriate headers to make the server refresh the resources.

I was just wondering if there was a global disable option within Cachecow because sometimes I forget to add the headers.

Thank you for getting back to me.

@circleupx
Copy link
Author

circleupx commented Jan 15, 2020

One more question,

Is it possible to set the cache expiration at runtime? Like I would omit the cache expiration from HttpCacheFactory then at runtime calculate the cache expiration base on the state of the resource.

@aliostad
Copy link
Owner

With the new rich configuration in .NET Core which is environment-aware, I guess we can do something about this now - the project was designed well before any of this.

I will flag it as a feature request and will start looking into it soon and will get back to you. Thanks for the suggestions.

@circleupx
Copy link
Author

@aliostad

Do you have any advice on my other question? Is this possible within CacheCow?

Is it possible to set the cache expiration at runtime? Like I would omit the cache expiration from HttpCacheFactory then at runtime calculate the cache expiration base on the state of the resource.

I have a REST API that may return multiple resource types base on the client request.

I would to set the expiration base the resource types within the HTTP response. For example, assume that an API has two resources, car and passenger and I decorate the controller like so,

public class CarController : Controller
{
    [HttpGet]
    [HttpCacheFactory(300, ViewModelType = typeof(Car))]
    public IActionResult Get(int id)
    {
        ... // implementation
    }
}

public class PassengerController : Controller
{
    [HttpGet]
    [HttpCacheFactory(60, ViewModelType = typeof(Passenger))]
    public IActionResult Get(int id)
    {
        ... // implementation
    }
}

The car resource has an expiration of 5 minutes, car information doesn't change that much. Passengers on the other hand is more volatile so I want to keep the cache time low, 1 minute.

Now, a client makes an HTTP request like so,

https://api.example.com/cars

With the current CacheCow configuration defined above, the server would response with Cache-Control: public, max-age=500.

Cool so far, everything works are expected and the correct cache expiration is set.

Now, assume that the car resource has a one to many relationship to passengers, and through ORM frameworks like EF CORE we can include then on our query should the client ask for resource.

Possible client request may look like this,

https://api.example.com/cars?include=passengers.

With the current CacheCow configuration defined above, the server would response with Cache-Control: public, max-age=500, but in this case I actually want to set the cache expiration to 1 minute because of the inclusion of passengers.

Thanks!

@aliostad
Copy link
Owner

Thanks for outlining your requirement with a clear example. Can you please open another issue with this particular question so it is also accessible for others?

ICacheDirectiveProvider interface and GetCacheControl method is responsible for providing the CacheControlHeader. It mainly operates on configuredExpiry but you can override it. Here is the implementation from DefaultCacheDirectiveProvider:

#if NET452
        public CacheControlHeaderValue GetCacheControl(HttpActionExecutedContext context, TimeSpan? configuredExpiry)
#else
        public CacheControlHeaderValue GetCacheControl(HttpContext context, TimeSpan? configuredExpiry)
#endif
        {
            switch (configuredExpiry)
            {
                case TimeSpan t when t == TimeSpan.Zero:
                    return new CacheControlHeaderValue() { MaxAge = TimeSpan.Zero, Private = true, MustRevalidate = true };
                case TimeSpan t:
                    return new CacheControlHeaderValue() { MaxAge = t, Public = true, MustRevalidate = true };
                default:
                    return new CacheControlHeaderValue() { NoCache = true, NoStore = true };
            }
        }
    }

So you need to create your own implementation which looks at the query string. Context parameter contains all information you need to make a decision.

You need to register your type. If you use the Generic interface means you can override for only when it returns a specific type which targets you method.

I could create an example but I cannot promise when exactly I will be able to.

@circleupx
Copy link
Author

Thank you, you have been very helpful.

aliostad added a commit that referenced this issue Jan 28, 2020
@aliostad aliostad mentioned this issue Jan 28, 2020
aliostad added a commit that referenced this issue Jan 28, 2020
@aliostad
Copy link
Owner

This is now done and will be released in 2.7.0. Have a look at docs here:
https://github.com/aliostad/CacheCow/blob/master/README.md#configuration-options-with-aspnet-core

Cheers
Ali

aliostad added a commit that referenced this issue Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants