apicache icon indicating copy to clipboard operation
apicache copied to clipboard

Allow requests to force cache to revalidate

Open ScottPolhemus opened this issue 3 years ago • 2 comments

This PR provides a mechanism for "revalidating" cache contents on a per-request basis. When a request contains the x-apicache-revalidate header, we skip loading from the cache (similar to a cache "bypass") but still make the response cacheable in order to update the cache store with fresh content.

I've found this to be useful in cases that would otherwise require manually clearing the cache before sending a new request in order to allow fresh content to be stored. In particular, I am implementing a "stale-while-revalidate" caching strategy in my application which serves stale content while revalidating in the background when the cached response is close to expiry, so that subsequent requests will receive more up-to-date content without having to incur the full processing overhead before sending a response to the client.

Note: I realized after-the-fact that this essentially duplicates the functionality from https://github.com/kwhitley/apicache/pull/220, which restored the original behavior for x-apicache-force-fetch which was regressed in https://github.com/kwhitley/apicache/pull/65. I personally find revalidate to be a more explicit description for the command (and less likely to be confused with bypass), but would be just as happy to see https://github.com/kwhitley/apicache/pull/220 merged in to restore this feature.

ScottPolhemus avatar May 14 '21 15:05 ScottPolhemus

Coverage Status

Coverage increased (+0.008%) to 97.546% when pulling eb1c67fa3bf30dc756097266243b064a5b8dcf96 on ScottPolhemus:revalidate into 3ae6fe70c329b4731638917e5f91fc1ab7c85d8e on kwhitley:master.

coveralls avatar May 14 '21 15:05 coveralls

I've been looking for this functionality in an express/redis lib and came across this. Any reason not to use the existing no-cache request header for this purpose ?

olsonpm avatar Apr 04 '22 22:04 olsonpm