Skip to content

Conversation

@man90es
Copy link
Contributor

@man90es man90es commented Feb 23, 2022

Motivation 1: my project that uses this library takes a rather long time to generate responses with status codes 404 and 503. It would be much more efficient to cache those responses, but the library just ignores responses with status codes >= 400 (this is hardcoded).

Motivation 2: status codes of responses served from cache are always 200, which seems like a bug. Response status codes may bring important information that API clients may rely upon. Replacing them all with 200 essentially destroys this information.

Changes 1: this PR implements status code filter option. It allows users to control responses with which status codes should be cached. If the option is not specified, everything below 400 will be cached, just like before, so it's 100% backward compatible.

Changes 2: This PR makes it so that status codes from original responses get saved in cache and reused later when served from it. This might not be backward compatible in some very specific use cases.

Example: this is a snippet of how I'd like to use this option in my project. It will cache all responses with status codes above and below 400, but not 400 (400 is used for format validation errors and can be generated quickly, so there's no need for me to cache it):

cacheClient, err := cache.NewClient(
	cache.ClientWithAdapter(memcached),
	cache.ClientWithTTL(time.Duration(*flagCacheTTL)*time.Minute),
	cache.ClientWithRefreshKey("opn"),
	cache.ClientWithStatusCodeFilter(func(code int) bool { return code != 400 }),
)

lazercube added a commit to rocketblend/echo-http-cache that referenced this pull request Aug 23, 2022
See [pull request](victorspringer/http-cache#13).

Co-authored-by: Elliot <elliotlunness@lazercube.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant