Feature/key builder - #427
Open
seracoder wants to merge 3 commits into
Open
Conversation
Add support for a callable key_builder(func, args, kwargs) -> str as an alternative to template-based key generation. The key_builder and key parameters are mutually exclusive. Closes Krukov#424.
…ock=True When key_builder bypasses the template system, both lock and cache decorators received the same raw key, causing set_lock to overwrite cached values. Fix by prefixing lock key_builder output with 'lock:'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds a
key_builderparameter to all cache and rate-limiting decorators, allowing users to supply a custom callable(func, args, kwargs) -> strfor cache key generation instead of relying on the template-based system. Closes #424.Changes
New
KeyBuilderprotocol (cashews/_typing.py):Callable[[Callable, tuple, dict], str]— receives the decorated function, positional args, and keyword args, returns the cache key string.Core key resolution (
cashews/key.py):get_cache_key()accepts an optionalkey_builder. When provided, it short-circuits template formatting and delegates to the callable directly.All cache strategy decorators updated with
key_builderparameter:cache,early,soft,failover,hit,iteratorlocked,rate_limit,slice_rate_limit,circuit_breakerbloom,dual_bloomWrapper methods (
cashews/wrapper/decorators.py):key_builderthrough to the underlying decorator.Design decisions
keyandkey_builderare mutually exclusive — raisesValueErrorif both are provided.key_builderis used (tags require template-based key resolution for registration), but tag resolution at call time still works.key_builderis used (it relies on template-based key generation).lock=Trueis combined withkey_builder, the lock key is automatically prefixed withlock:to prevent collision with the cache key.Usage
Testing