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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Daniel-Chin
Copy link

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.

@Daniel-Chin Daniel-Chin requested a review from shaypal5 as a code owner March 28, 2025 14:51
@shaypal5
Copy link
Collaborator

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

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.

2 participants