-
-
Notifications
You must be signed in to change notification settings - Fork 66
feat: add cache_contains() for read-only key lookup #746
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
rodrigobnogueira
merged 3 commits into
aio-libs:master
from
rahulkaushal04:feature/cache-contains
Mar 16, 2026
+159
−0
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
08f54ed
feat: add cache_contains method to check cache presence without affec…
rahulkaushal04 a52d05f
docs: update README and tests to include examples for cache_contains …
rahulkaushal04 252aa4f
refactor: add type hints to coro functions in cache_contains tests
rahulkaushal04 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
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,128 @@ | ||
| import asyncio | ||
| from async_lru import alru_cache | ||
|
|
||
|
|
||
| async def test_cache_contains_basic(check_lru): | ||
|
rahulkaushal04 marked this conversation as resolved.
Outdated
|
||
| @alru_cache(maxsize=4) | ||
| async def coro(val): | ||
| return val | ||
|
|
||
| assert coro.cache_contains(1) is False | ||
| await coro(1) | ||
| assert coro.cache_contains(1) is True | ||
| assert coro.cache_contains(2) is False | ||
| check_lru(coro, hits=0, misses=1, cache=1, tasks=0, maxsize=4) | ||
|
|
||
|
|
||
| async def test_cache_contains_does_not_affect_counters(check_lru): | ||
| @alru_cache(maxsize=4) | ||
| async def coro(val): | ||
| return val | ||
|
|
||
| await coro(1) | ||
| for _ in range(10): | ||
| coro.cache_contains(1) | ||
| coro.cache_contains(99) | ||
|
|
||
| # hits/misses must stay unchanged after cache_contains calls | ||
| check_lru(coro, hits=0, misses=1, cache=1, tasks=0, maxsize=4) | ||
|
|
||
|
|
||
| async def test_cache_contains_does_not_change_lru_order(): | ||
| @alru_cache(maxsize=2) | ||
| async def coro(val): | ||
| return val | ||
|
|
||
| await coro(1) | ||
| await coro(2) | ||
|
|
||
| # Peek at key 1 without refreshing its LRU position | ||
| assert coro.cache_contains(1) is True | ||
|
|
||
| # Adding a third entry must evict key 1 (oldest), not key 2 | ||
| await coro(3) | ||
|
|
||
| assert coro.cache_contains(1) is False | ||
| assert coro.cache_contains(2) is True | ||
| assert coro.cache_contains(3) is True | ||
|
|
||
|
|
||
| async def test_cache_contains_after_invalidate_and_clear(): | ||
| @alru_cache(maxsize=4) | ||
| async def coro(val): | ||
| return val | ||
|
|
||
| await coro(1) | ||
| await coro(2) | ||
|
|
||
| coro.cache_invalidate(1) | ||
| assert coro.cache_contains(1) is False | ||
| assert coro.cache_contains(2) is True # unaffected | ||
|
|
||
| coro.cache_clear() | ||
| assert coro.cache_contains(2) is False | ||
|
|
||
|
|
||
| async def test_cache_contains_with_kwargs(): | ||
| @alru_cache(maxsize=4) | ||
| async def coro(a, b=10): | ||
| return a + b | ||
|
|
||
| await coro(1, b=20) | ||
| assert coro.cache_contains(1, b=20) is True | ||
| assert coro.cache_contains(1, b=30) is False | ||
| assert coro.cache_contains(1) is False | ||
|
|
||
|
|
||
| async def test_cache_contains_respects_typed_flag(): | ||
|
|
||
|
rahulkaushal04 marked this conversation as resolved.
Outdated
|
||
| @alru_cache(maxsize=4, typed=True) | ||
| async def coro(val): | ||
| return val | ||
|
|
||
| await coro(1) | ||
| assert coro.cache_contains(1) is True | ||
| assert coro.cache_contains(1.0) is False | ||
|
|
||
|
|
||
| async def test_cache_contains_pending_task(): | ||
| event = asyncio.Event() | ||
|
|
||
| @alru_cache(maxsize=4) | ||
| async def coro(val): | ||
| await event.wait() | ||
| return val | ||
|
|
||
| task = asyncio.ensure_future(coro(1)) | ||
| await asyncio.sleep(0) | ||
|
|
||
| # Key must be present even while the underlying task is still running | ||
| assert coro.cache_contains(1) is True | ||
|
|
||
| event.set() | ||
| await task | ||
|
|
||
|
|
||
| async def test_cache_contains_after_ttl_expiry(): | ||
| @alru_cache(maxsize=4, ttl=0.05) | ||
| async def coro(val): | ||
| return val | ||
|
|
||
| await coro(1) | ||
| assert coro.cache_contains(1) is True | ||
|
|
||
| await asyncio.sleep(0.1) | ||
| assert coro.cache_contains(1) is False | ||
|
|
||
|
|
||
| async def test_cache_contains_on_method(): | ||
| class MyService: | ||
| @alru_cache(maxsize=4) | ||
| async def fetch(self, key): | ||
| return key * 2 | ||
|
|
||
| svc = MyService() | ||
| await svc.fetch(5) | ||
|
|
||
| assert svc.fetch.cache_contains(5) is True | ||
| assert svc.fetch.cache_contains(6) is False | ||
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.