Skip to content

Feature/key prefix propagation to redis core and global prefix override#395

Open
matteomorlack wants to merge 6 commits into
python-cachier:masterfrom
matteomorlack:feature/key-prefix
Open

Feature/key prefix propagation to redis core and global prefix override#395
matteomorlack wants to merge 6 commits into
python-cachier:masterfrom
matteomorlack:feature/key-prefix

Conversation

@matteomorlack

Copy link
Copy Markdown

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.

@matteomorlack matteomorlack requested a review from shaypal5 as a code owner July 7, 2026 22:48
@Borda Borda requested a review from Copilot July 9, 2026 13:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_prefix and func_prefix to global Params so they can be set via set_global_params(...).
  • Add key_prefix to the @cachier decorator 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.

Comment thread src/cachier/cores/base.py Outdated
Comment on lines +38 to +40

"""
return f".{func.__module__}.{func.__name__}"
return f"{_update_with_defaults('.', 'func_prefix')}{func.__module__}.{func.__name__}"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fixed with be1faaf

Comment thread src/cachier/core.py
Comment on lines 222 to 230
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,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

included in docstring 6cd8e5d

Comment thread src/cachier/core.py
Comment on lines 357 to 413
# 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,
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests here 1108356

@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.95%. Comparing base (422f890) to head (a1fccd0).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@           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           
Flag Coverage Δ
local 66.61% <100.00%> (+0.04%) ⬆️
mongodb 40.10% <100.00%> (+0.08%) ⬆️
postgres 42.18% <100.00%> (+0.12%) ⬆️
redis 44.64% <100.00%> (+0.07%) ⬆️
s3 41.38% <100.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/cachier/config.py 100.00% <100.00%> (ø)
src/cachier/core.py 100.00% <100.00%> (ø)
src/cachier/cores/base.py 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 422f890...a1fccd0. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants