Feature/key prefix propagation to redis core and global prefix override#395
Feature/key prefix propagation to redis core and global prefix override#395matteomorlack wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #394 by adding a globally configurable key_prefix and ensuring it is propagated from the @cachier decorator into the Redis backend, enabling consistent key namespacing across Redis caches.
Changes:
- Add
key_prefixandfunc_prefixto globalParamsso they can be set viaset_global_params(...). - Add
key_prefixto the@cachierdecorator and pass the resolved value into_RedisCore. - Use a configurable function-string prefix when building per-function identifiers used by cores.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/test_core_lookup.py | Updates the expected global params list to include func_prefix and key_prefix. |
| src/cachier/cores/base.py | Switches function identifier formatting to use a configurable prefix (via global params). |
| src/cachier/core.py | Introduces key_prefix decorator arg, resolves via _update_with_defaults, and passes into _RedisCore. |
| src/cachier/config.py | Extends global Params with key_prefix and func_prefix defaults. |
|
|
||
| """ | ||
| return f".{func.__module__}.{func.__name__}" | ||
| return f"{_update_with_defaults('.', 'func_prefix')}{func.__module__}.{func.__name__}" |
| def cachier( | ||
| hash_func: Optional[HashFunc] = None, | ||
| hash_params: Optional[HashFunc] = None, | ||
| backend: Optional[Backend] = None, | ||
| mongetter: Optional[Mongetter] = None, | ||
| sql_engine: Optional[Union[str, Any, Callable[[], Any]]] = None, | ||
| redis_client: Optional["RedisClient"] = None, | ||
| key_prefix: Optional[str] = None, | ||
| s3_bucket: Optional[str] = None, |
| # Update parameters with defaults if input is None | ||
| backend = _update_with_defaults(backend, "backend") | ||
| mongetter = _update_with_defaults(mongetter, "mongetter") | ||
| size_limit_bytes = parse_bytes(_update_with_defaults(entry_size_limit, "entry_size_limit")) | ||
| key_prefix = _update_with_defaults(key_prefix, "key_prefix") | ||
|
|
||
| # Create metrics object if enabled | ||
| cache_metrics = None | ||
| if enable_metrics: | ||
| cache_metrics = CacheMetrics(sampling_rate=metrics_sampling_rate) | ||
|
|
||
| # Override the backend parameter if a mongetter is provided. | ||
| if callable(mongetter): | ||
| backend = "mongo" | ||
| core: _BaseCore | ||
| if backend == "pickle": | ||
| core = _PickleCore( | ||
| hash_func=hash_func, | ||
| pickle_reload=pickle_reload, | ||
| cache_dir=cache_dir, | ||
| separate_files=separate_files, | ||
| wait_for_calc_timeout=wait_for_calc_timeout, | ||
| entry_size_limit=size_limit_bytes, | ||
| metrics=cache_metrics, | ||
| ) | ||
| elif backend == "mongo": | ||
| core = _MongoCore( | ||
| hash_func=hash_func, | ||
| mongetter=mongetter, | ||
| wait_for_calc_timeout=wait_for_calc_timeout, | ||
| entry_size_limit=size_limit_bytes, | ||
| metrics=cache_metrics, | ||
| ) | ||
| elif backend == "memory": | ||
| core = _MemoryCore( | ||
| hash_func=hash_func, | ||
| wait_for_calc_timeout=wait_for_calc_timeout, | ||
| entry_size_limit=size_limit_bytes, | ||
| metrics=cache_metrics, | ||
| ) | ||
| elif backend == "sql": | ||
| core = _SQLCore( | ||
| hash_func=hash_func, | ||
| sql_engine=sql_engine, | ||
| wait_for_calc_timeout=wait_for_calc_timeout, | ||
| entry_size_limit=size_limit_bytes, | ||
| metrics=cache_metrics, | ||
| ) | ||
| elif backend == "redis": | ||
| core = _RedisCore( | ||
| hash_func=hash_func, | ||
| redis_client=redis_client, | ||
| wait_for_calc_timeout=wait_for_calc_timeout, | ||
| entry_size_limit=size_limit_bytes, | ||
| metrics=cache_metrics, | ||
| key_prefix=key_prefix, | ||
| ) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #395 +/- ##
=======================================
Coverage 99.95% 99.95%
=======================================
Files 16 16
Lines 2109 2112 +3
Branches 251 251
=======================================
+ Hits 2108 2111 +3
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
Proposal fix to #394 in order to let RedisCore receive the key_prefix param with correct default update and to let a global default prefix be used.