-
Notifications
You must be signed in to change notification settings - Fork 973
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
Changes from 2 commits
fd6026c
9d295b2
f247a5d
8838dc6
42c700f
b00eec2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # 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(): | ||
| print("REMOVE PRINT STATEMENT") | ||
|
stevejalim marked this conversation as resolved.
Outdated
|
||
| 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 | ||
|
stevejalim marked this conversation as resolved.
|
||
|
|
||
|
|
||
| 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" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,7 +90,6 @@ def data_path(*args): | |
| CACHE_TIME_MED = 60 * 60 # 1 hour | ||
| CACHE_TIME_LONG = 60 * 60 * 6 # 6 hours | ||
|
|
||
|
|
||
| CACHES = { | ||
| "default": { | ||
| "BACKEND": "bedrock.base.cache.SimpleDictCache", | ||
|
|
@@ -101,6 +100,13 @@ def data_path(*args): | |
| "CULL_FREQUENCY": 4, # 1/4 entries deleted if max reached | ||
| }, | ||
| }, | ||
| "db": { | ||
| # Intended for use as a slower – but distributed - cache | ||
| # See bedrock.base.cache.get_from_hybrid_cache and set_in_hybrid_cache | ||
| "LOCATION": "hybrid_cache_db_table", # name of DB table to be used | ||
| "BACKEND": "django.core.cache.backends.db.DatabaseCache", | ||
| "TIMEOUT": None, # cached items will not expire | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want this to be forever? Have we considered a long timeout so they eventually do get purged? No expiry ever seems like this table would potentially bloat over time.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the default timeout for the cache, so that we can support the idea of never-expiring things, but Related to this, though, I've just thought about providing more flexibility in speccing cache times for db and locmem cacheing when using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the cache backends in Django, they use The backends also define a My thought is we should use a very long expiry as the default, even if it's like 1 year or something, just so old unused cache does get purged. And have to explicitly set
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, great - if it is possible to override the default with |
||
| }, | ||
| } | ||
|
|
||
| # Logging | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.