Add support for negative caching to cache.Cache #93
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit adds support for negative caching, that is: caching the non-existence of a specific key. This can help to reduce the cost of cache misses when a client is repeatedly requesting a non-existent object, and may also help mitigate denial of service attacks which target the relatively expensive cache fill operation.
The change is backwards compatible with the existing interface for NewCache through the magic of functional options. Negative caching is disabled by default, but can be enabled by specifying a
WithNegativeCaching
option at initialization:To use negative caching, the cache fetcher function must return a sentinel error value,
cache.ErrDoesNotExist
. This is also the error that will be returned for a negative result. Clients should useerrors.Is(err, cache.ErrDoesNotExist)
to identify a negative result.