HttpClient.Caching icon indicating copy to clipboard operation
HttpClient.Caching copied to clipboard

[Enhancement] Disable Cache for request

Open ThaDaVos opened this issue 1 year ago • 3 comments

Summary

Please provide a brief summary of your proposal. Two to three sentences is best here.

The option to disable the caching for a request - preferably using HttpRequestMessage.Options.Set() to set a value on the HttpRequestMessage which disables caching for a single request/response.

Why? Cause you can't change handlers after the Client is initiated and I don't want to create multiple HttpClients for this.

API Changes

Include a list of all API changes, additions, subtractions as would be required by your proposal.

e.g.

In order for this to work - the IF statement which checks if it's inside the cache also needs to check HttpRequestMessage.Options if the option to skip cache has been set https://github.com/thomasgalliker/HttpClient.Caching/blob/f2831a70b6d896fb54e401110127e05f5c131269/HttpClient.Caching/InMemory/InMemoryCacheHandler.cs#L109

public readonly HttpRequestOptionsKey<bool> UseCache = new("UseCache");

....

// Inside SendAsync and Send

if (!(request.Options.TryGetValue(UseCache, out bool useCache) && useCache) && (request.Method == HttpMethod.Get || request.Method == HttpMethod.Head))

Intended Use Case

Provide a detailed example of where your proposal would be used and for what purpose.

It can be used to force up date the cache depending if the above IF change is also included in the IF statement for storing the response - second it can be used to skip the cache all together when needed.

ThaDaVos avatar Jul 03 '24 09:07 ThaDaVos

Cool idea @ThaDaVos I gonna have to put some thinking into it. Aren‘t there also cache headers in http requests which could be used for this purpose?

thomasgalliker avatar Jul 07 '24 09:07 thomasgalliker

Already got stuff done on a fork - will create a pull-request later so you can check

ThaDaVos avatar Jul 07 '24 09:07 ThaDaVos

See: https://github.com/thomasgalliker/HttpClient.Caching/pull/19

ThaDaVos avatar Jul 11 '24 08:07 ThaDaVos