Skip to content

Add feature: call with freshness threshold #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 4, 2025

Conversation

Daniel-Chin
Copy link
Contributor

The feature

In addition to specifying shelf life at storage time, this supports specifying max age at retrieval time.

Motivations

This is useful in interactive projects / data science notebooks.
I personally came to need this when building a cachable API client for Semantic Scholar. (Who knows when's the next groundbreaking paper? I only know how much cache to trust in hindsight.)

How to use

Refer to the test:

def test_call_with_freshness_threshold():
    @cachier.cachier()
    def test_func(a, b):
        print('Computing...')
        return a + b
    
    print(f'{test_func(1, 2) = }')
    print(f'{test_func(1, 2) = }')
    caller_with_freshness_threshold = test_func.caller_with_freshness_threshold(
        timedelta(seconds=0.5), 
    )
    print(f'{caller_with_freshness_threshold(1, 2) = }')
    print(f'{time.sleep(1.0) = }')
    print(f'{test_func(1, 2) = }')
    print(f'{caller_with_freshness_threshold(1, 2) = }')

Anecdote

        def _caller_with_freshness_threshold(max_age: timedelta):
            return wraps(func)(partial(_call, max_age))
        
        func_wrapper.caller_with_freshness_threshold = _caller_with_freshness_threshold

can in fact be further reduced to

func_wrapper.caller_with_freshness_threshold = partial(partial, _call)

(how fun!) but then how do you apply wraps(func) to type hint it? I haven't the faintest.

@shaypal5
Copy link
Member

Hey Daniel, :)

A very cool feature! Thank you for contributing.
I approved tests to run for your PR, and it seems you broke ignoring of self in certain cases.

Please try and solve this. And tag me or @Borda if you need help.

Cheers!
@Daniel-Chin

Comment on lines +13 to +23
print(f"{test_func(1, 2) = }")
print(f"{test_func(1, 2) = }")
caller_with_freshness_threshold = (
test_func.caller_with_freshness_threshold(
timedelta(seconds=0.5),
)
)
print(f"{caller_with_freshness_threshold(1, 2) = }")
print(f"{time.sleep(1.0) = }")
print(f"{test_func(1, 2) = }")
print(f"{caller_with_freshness_threshold(1, 2) = }")
Copy link
Contributor

Choose a reason for hiding this comment

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

are all the prints needed, why just not assert for expected outcomes?

@Borda
Copy link
Contributor

Borda commented May 28, 2025

Please try and solve this. And tag me or @Borda if you need help.

may need some more debugging... 🤔

@Borda Borda temporarily deployed to safety_check June 2, 2025 11:33 — with GitHub Actions Inactive
@Borda
Copy link
Contributor

Borda commented Jun 16, 2025

@Daniel-Chin are you still working on it? 🦩

@shaypal5 shaypal5 changed the base branch from master to freshness July 4, 2025 14:25
@shaypal5 shaypal5 merged commit 68544a6 into python-cachier:freshness Jul 4, 2025
1 check was pending
shaypal5 added a commit that referenced this pull request Jul 5, 2025
* Add feature: call with freshness threshold (#262)

* 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]>

* fixin things up

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

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

* fix max_age bug

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

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

* fixes

* change max_age condition to strictly smalled

* add a short sleep on test_max_age_zero

* more sleep

* neg max age means cached values are considered stale

* some renaming

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

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

* one of the max_age tests needs debugging

* sleep for neg max_age test

* Update src/cachier/core.py

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: Daniel Chin <[email protected]>
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: Copilot <[email protected]>
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.

3 participants