Skip to content

Add feature: Call with freshness threshold (#262)#271

Merged
shaypal5 merged 15 commits intomasterfrom
freshness
Jul 5, 2025
Merged

Add feature: Call with freshness threshold (#262)#271
shaypal5 merged 15 commits intomasterfrom
freshness

Conversation

@shaypal5
Copy link
Copy Markdown
Member

@shaypal5 shaypal5 commented Jul 4, 2025

Add the ability to require a minimal degree of freshness from a returned cached value on call/retrieval (unlike setting stale_after when storing a value in the cache); if the cached value is not fresh enough, a the cached function is called instead.

* add caller_with_freshness_threshold

* fix type

* simple test passed

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Apply suggestions from code review

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Shay Palachy-Affek <[email protected]>
@shaypal5 shaypal5 marked this pull request as draft July 4, 2025 14:31
@shaypal5 shaypal5 self-assigned this Jul 4, 2025
@shaypal5 shaypal5 requested a review from Copilot July 4, 2025 15:53

This comment was marked as outdated.

@shaypal5 shaypal5 requested a review from Copilot July 4, 2025 19:59

This comment was marked as outdated.

@shaypal5 shaypal5 marked this pull request as ready for review July 5, 2025 14:50
@shaypal5 shaypal5 requested review from Borda and Copilot July 5, 2025 14:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds a per-call freshness threshold (max_age) that forces cache recalculation when a cached value exceeds this age, integrating it with the existing stale_after logic.

  • Introduce max_age parameter in core wrapper, computing an effective age threshold as min(stale_after, max_age)
  • Update tests to cover stricter/looser/zero/negative max_age scenarios
  • Document max_age in README and register a new pytest marker

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.

File Description
tests/test_call_with_max_age.py New tests for various max_age use cases
src/cachier/core.py Core caching logic extended to handle max_age
pyproject.toml Registered maxage pytest marker
README.rst Added Per-call max age section
Comments suppressed due to low confidence (2)

tests/test_call_with_max_age.py:62

  • [nitpick] Add a test where max_age exactly equals the decorator's stale_after to verify boundary behavior and ensure it meets expectations.
    v3 = f(1, max_age=timedelta(seconds=5))

README.rst:243

  • Indent the example code block lines by at least four spaces under the .. code-block:: python directive to ensure proper formatting in the rendered docs.
  from datetime import timedelta

@shaypal5
Copy link
Copy Markdown
Member Author

shaypal5 commented Jul 5, 2025

Now this is ready to review (based on @Daniel-Chin 's original PR, but adapted, fixed and thoroughly tested and documented - all, of course, mainly due to cursor).
@Borda

@shaypal5 shaypal5 merged commit d7b0c17 into master Jul 5, 2025
37 checks passed
@shaypal5 shaypal5 deleted the freshness branch July 5, 2025 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants