-
Notifications
You must be signed in to change notification settings - Fork 72
Add feature: Call with freshness threshold (#262) #271
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
68544a6
Add feature: call with freshness threshold (#262)
Daniel-Chin 40b318e
fixin things up
shaypal5 5ef6ae1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4f2ee51
fix max_age bug
shaypal5 1205a68
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c37500d
fixes
shaypal5 366d1db
change max_age condition to strictly smalled
shaypal5 b368807
add a short sleep on test_max_age_zero
shaypal5 3087d97
more sleep
shaypal5 23a8146
neg max age means cached values are considered stale
shaypal5 ee22fa1
some renaming
shaypal5 2015cc6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a7f08b4
one of the max_age tests needs debugging
shaypal5 2eacb33
sleep for neg max_age test
shaypal5 bb353b8
Update src/cachier/core.py
shaypal5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,3 +106,5 @@ Pipfile.lock | |
| .DS_Store | ||
|
|
||
| tags | ||
|
|
||
| .cursor | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import time | ||
| from datetime import timedelta | ||
|
|
||
| import pytest | ||
|
|
||
| import cachier | ||
|
|
||
|
|
||
| @pytest.mark.maxage | ||
| def test_call_with_max_age(): | ||
| @cachier.cachier() | ||
| def test_func(a, b): | ||
| return a + b | ||
|
|
||
| # First call: should compute and cache | ||
| val1 = test_func(1, 2) | ||
| assert val1 == 3 | ||
| # Second call: should use cache | ||
| val2 = test_func(1, 2) | ||
| assert val2 == 3 | ||
| # Wait for cache to become stale | ||
| time.sleep(1.0) | ||
| # Should trigger recalculation (stale) | ||
| val3 = test_func(1, 2, max_age=timedelta(seconds=0.5)) | ||
| assert val3 == 3 | ||
|
|
||
|
|
||
| @pytest.mark.maxage | ||
| def test_max_age_stricter_than_stale_after(): | ||
| import time | ||
|
|
||
| import cachier | ||
|
|
||
| @cachier.cachier(stale_after=timedelta(seconds=2)) | ||
| def f(x): | ||
| return time.time() | ||
|
|
||
| f.clear_cache() | ||
| v1 = f(1) | ||
| v2 = f(1) | ||
| assert v1 == v2 # cache hit | ||
| time.sleep(1) | ||
| v3 = f(1, max_age=timedelta(seconds=0.5)) | ||
| assert v3 != v1 # max_age stricter, triggers recalc | ||
|
|
||
|
|
||
| @pytest.mark.maxage | ||
| def test_max_age_looser_than_stale_after(): | ||
| import time | ||
|
|
||
| import cachier | ||
|
|
||
| @cachier.cachier(stale_after=timedelta(seconds=1)) | ||
| def f(x): | ||
| return time.time() | ||
|
|
||
| f.clear_cache() | ||
| v1 = f(1) | ||
| v2 = f(1) | ||
| assert v1 == v2 | ||
| time.sleep(1.1) | ||
| v3 = f(1, max_age=timedelta(seconds=5)) | ||
| assert v3 != v1 # max_age looser, but stale_after still applies (stricter) | ||
|
|
||
|
|
||
| @pytest.mark.maxage | ||
| def test_max_age_none_defaults_to_stale_after(): | ||
| import time | ||
|
|
||
| import cachier | ||
|
|
||
| @cachier.cachier(stale_after=timedelta(seconds=1)) | ||
| def f(x): | ||
| return time.time() | ||
|
|
||
| f.clear_cache() | ||
| v1 = f(1) | ||
| time.sleep(1.1) | ||
| v2 = f(1, max_age=None) | ||
| assert v2 != v1 # Should trigger recalc (stale_after applies) | ||
|
|
||
|
|
||
| @pytest.mark.maxage | ||
| def test_negative_max_age_triggers_recalc(): | ||
| import time | ||
|
|
||
| import cachier | ||
|
|
||
| @cachier.cachier(stale_after=timedelta(seconds=100)) | ||
| def f(x): | ||
| return time.time() | ||
|
|
||
| f.clear_cache() | ||
| v1 = f(1) | ||
| time.sleep(0.5) # Ensure some time has passed | ||
| v2 = f(1, max_age=timedelta(seconds=-1), cachier__verbose=True) | ||
| assert v2 != v1 # Negative max_age always triggers recalc | ||
|
|
||
|
|
||
| @pytest.mark.maxage | ||
| def test_max_age_zero(): | ||
| import time | ||
|
|
||
| import cachier | ||
|
|
||
| @cachier.cachier(stale_after=timedelta(seconds=100)) | ||
| def f(x): | ||
| return time.time() | ||
|
|
||
| f.clear_cache() | ||
| v1 = f(1) | ||
| # Add a small sleep to ensure measurable time difference on all platforms | ||
| time.sleep(1) | ||
| v2 = f(1, max_age=timedelta(seconds=0)) | ||
| assert v2 != v1 # Zero max_age always triggers recalc | ||
|
|
||
|
|
||
| @pytest.mark.maxage | ||
| def test_max_age_with_next_time(): | ||
| import time | ||
|
|
||
| import cachier | ||
|
|
||
| @cachier.cachier(stale_after=timedelta(seconds=1), next_time=True) | ||
| def f(x): | ||
| return time.time() | ||
|
|
||
| f.clear_cache() | ||
| v1 = f(1) | ||
| time.sleep(1.1) | ||
| v2 = f(1, max_age=timedelta(seconds=0.5)) | ||
| # With next_time=True, should return stale value (v1) while | ||
| # triggering a recalculation in the background | ||
| assert v2 == v1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.