-
Notifications
You must be signed in to change notification settings - Fork 957
Add helpers to support a 'hybrid cache' option that uses locmem + DB cache #15859
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd6026c
Add helpers to support a 'hybrid cache' option that uses locmem + DB …
stevejalim 9d295b2
Add support to the sqlite export command to boostrap the DB cache tab…
stevejalim f247a5d
Remove debugging print statement
stevejalim 8838dc6
Comment fixups
stevejalim 42c700f
Improve configurability of hybrid cache timeouts + add relevant tests
stevejalim b00eec2
Set a very long default cache time for the DB-backed cache, which can…
stevejalim 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,137 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| from django.conf import settings | ||
| from django.core.cache import caches | ||
|
|
||
| import pytest | ||
|
|
||
| from bedrock.base.cache import get_from_hybrid_cache, set_in_hybrid_cache | ||
|
|
||
| pytestmark = [ | ||
| pytest.mark.django_db, | ||
| ] | ||
|
|
||
| local_cache = caches["default"] | ||
| db_cache = caches["db"] | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def clear_caches(): | ||
| local_cache.clear() | ||
| db_cache.clear() | ||
|
|
||
|
|
||
| def test_hybrid_cache_get(): | ||
| key = "test_key" | ||
| value = "test_value" | ||
|
|
||
| local_cache.set( | ||
| key, | ||
| value, | ||
| timeout=settings.CACHE_TIME_SHORT, | ||
| ) | ||
| db_cache.set( | ||
| key, | ||
| value, | ||
| timeout=settings.CACHE_TIME_SHORT, | ||
| ) | ||
|
|
||
| # Test getting from local cache directly | ||
| assert get_from_hybrid_cache(key) == value | ||
|
|
||
| # Test falling back to db cache and populating local cache | ||
| local_cache.clear() | ||
| assert local_cache.get(key) is None | ||
| assert db_cache.get(key) == value | ||
|
|
||
| assert get_from_hybrid_cache(key) == value | ||
| assert local_cache.get(key) == value | ||
|
|
||
|
|
||
| def test_hybrid_cache_get_no_values_in_local_or_db_cache(): | ||
| key = "test_key" | ||
|
|
||
| assert local_cache.get(key) is None | ||
| assert db_cache.get(key) is None | ||
| assert get_from_hybrid_cache(key) is None | ||
|
|
||
|
|
||
| def test_hybrid_cache_get__default_value(): | ||
| # Test getting default value when key is not found | ||
| assert ( | ||
| get_from_hybrid_cache( | ||
| "non_existent_key", | ||
| default="default_value", | ||
| ) | ||
| == "default_value" | ||
| ) | ||
|
|
||
|
|
||
| def test_hybrid_cache_set(): | ||
| key = "test_key" | ||
| value = "test_value" | ||
| set_in_hybrid_cache(key, value) | ||
|
|
||
| assert local_cache.get(key) == value | ||
| assert db_cache.get(key) == value | ||
|
|
||
|
|
||
| def test_set_in_hybrid_cache_db_cache_failure(caplog, mocker): | ||
| key = "test_key_db_failure" | ||
| value = "test_value_db_failure" | ||
| timeout = 60 | ||
|
|
||
| mocker.patch.object( | ||
| db_cache, | ||
| "set", | ||
| side_effect=Exception("Faked DB cache failure"), | ||
| ) | ||
|
|
||
| set_in_hybrid_cache(key, value, timeout) | ||
|
|
||
| assert local_cache.get(key) == value | ||
| assert db_cache.get(key) is None | ||
|
|
||
| assert caplog.records[0].msg == "Could not set value in DB-backed cache: Faked DB cache failure" | ||
|
|
||
|
|
||
| def test_set_in_hybrid_cache_default_timeouts(mocker): | ||
| key = "test_key" | ||
| value = "test_value" | ||
|
|
||
| mock_db_set = mocker.patch.object(caches["db"], "set") | ||
| mock_local_set = mocker.patch.object(caches["default"], "set") | ||
|
|
||
| set_in_hybrid_cache(key, value) | ||
|
|
||
| mock_db_set.assert_called_once_with(key, value, timeout=None) | ||
| mock_local_set.assert_called_once_with(key, value, timeout=settings.CACHE_TIME_SHORT) | ||
|
|
||
|
|
||
| def test_set_in_hybrid_cache_custom_db_cache_timeout(mocker): | ||
| key = "test_key" | ||
| value = "test_value" | ||
| custom_db_cache_timeout = 120 | ||
|
|
||
| mock_db_set = mocker.patch.object(caches["db"], "set") | ||
| mock_local_set = mocker.patch.object(caches["default"], "set") | ||
| set_in_hybrid_cache(key, value, db_cache_timeout=custom_db_cache_timeout) | ||
|
|
||
| mock_db_set.assert_called_once_with(key, value, timeout=custom_db_cache_timeout) | ||
| mock_local_set.assert_called_once_with(key, value, timeout=settings.CACHE_TIME_SHORT) | ||
|
|
||
|
|
||
| def test_set_in_hybrid_cache_custom_locmem_cache_timeout(mocker): | ||
| key = "test_key" | ||
| value = "test_value" | ||
| custom_locmem_cache_timeout = 42 | ||
|
|
||
| mock_db_set = mocker.patch.object(caches["db"], "set") | ||
| mock_local_set = mocker.patch.object(caches["default"], "set") | ||
|
|
||
| set_in_hybrid_cache(key, value, locmem_cache_timeout=custom_locmem_cache_timeout) | ||
|
|
||
| mock_db_set.assert_called_once_with(key, value, timeout=None) | ||
| mock_local_set.assert_called_once_with(key, value, timeout=custom_locmem_cache_timeout) | ||
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
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.