-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
213867a
commit 852067a
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains 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,46 @@ | ||
from django.core.cache import caches | ||
from django.core.cache.backends.base import BaseCache | ||
|
||
|
||
class FallbackCache(BaseCache): | ||
""" | ||
Cache backend that supports a list of fallback caches | ||
This cache backend sets the value in all caches and reads from caches in order | ||
until it gets a hit. You'll typically want to configure this with a list of | ||
caches with increasing durability. | ||
For example in settings.py: | ||
CACHES = { | ||
"fallback": { | ||
"BACKEND": "main.cache.backends.FallbackCache", | ||
"LOCATION": ["in-memory", "redis", "database"], | ||
}, | ||
... | ||
} | ||
""" | ||
|
||
def __init__(self, cache_names, params): | ||
super().__init__(params) | ||
self._cache_names = cache_names | ||
|
||
def get(self, key, default=None, version=None): | ||
"""Get the value from the caches in order""" | ||
for cache_name in self._cache_names: | ||
cache = caches[cache_name] | ||
result = cache.get(key, default=default, version=version) | ||
if result: | ||
return result | ||
return None | ||
|
||
def set(self, key, value, timeout=None, version=None): | ||
"""Set a value in the caches""" | ||
for cache_name in self._cache_names: | ||
cache = caches[cache_name] | ||
cache.set( | ||
key, | ||
value, | ||
timeout=timeout, | ||
version=version, | ||
) |
This file contains 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,44 @@ | ||
from django.core.cache.backends.base import BaseCache | ||
|
||
from main.cache.backends import FallbackCache | ||
|
||
|
||
def test_fallback_cache_get(mocker, settings): | ||
"""Test that get() on the fallback cache works correctly""" | ||
mock_cache_1 = mocker.Mock(spec=BaseCache) | ||
mock_cache_1.get.return_value = 12345 | ||
mock_cache_2 = mocker.Mock(spec=BaseCache) | ||
mock_cache_2.get.return_value = 67890 | ||
|
||
mocker.patch.dict( | ||
"main.cache.backends.caches", {"dummy1": mock_cache_1, "dummy2": mock_cache_2} | ||
) | ||
|
||
cache = FallbackCache(["dummy1", "dummy2"], {}) | ||
|
||
assert cache.get("key", default="default", version=1) == 12345 | ||
|
||
mock_cache_1.get.return_value = None | ||
|
||
assert cache.get("key", default="default", version=1) == 67890 | ||
|
||
mock_cache_2.get.return_value = None | ||
|
||
assert cache.get("key", default="default", version=1) is None | ||
|
||
|
||
def test_fallback_cache_set(mocker, settings): | ||
"""Test that set() on the fallback cache works correctly""" | ||
mock_cache_1 = mocker.Mock(spec=BaseCache) | ||
mock_cache_2 = mocker.Mock(spec=BaseCache) | ||
|
||
mocker.patch.dict( | ||
"main.cache.backends.caches", {"dummy1": mock_cache_1, "dummy2": mock_cache_2} | ||
) | ||
|
||
cache = FallbackCache(["dummy1", "dummy2"], {}) | ||
|
||
cache.set("key", "value", timeout=600, version=1) | ||
|
||
mock_cache_1.set.assert_called_once_with("key", "value", timeout=600, version=1) | ||
mock_cache_2.set.assert_called_once_with("key", "value", timeout=600, version=1) |
This file contains 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 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