Skip to content

Commit

Permalink
Add caching for imagekit (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysyngsun authored Aug 28, 2024
1 parent 213867a commit 852067a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
Empty file added main/cache/__init__.py
Empty file.
46 changes: 46 additions & 0 deletions main/cache/backends.py
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,
)
44 changes: 44 additions & 0 deletions main/cache/backends_test.py
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)
19 changes: 19 additions & 0 deletions main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@

IMAGEKIT_SPEC_CACHEFILE_NAMER = "imagekit.cachefiles.namers.source_name_dot_hash"
IMAGEKIT_CACHEFILE_DIR = get_string("IMAGEKIT_CACHEFILE_DIR", "")
IMAGEKIT_CACHE_BACKEND = "imagekit"


# django cache back-ends
Expand All @@ -532,6 +533,24 @@
"LOCATION": CELERY_BROKER_URL, # noqa: F405
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
},
# imagekit caching
"imagekit": {
"BACKEND": "main.cache.backends.FallbackCache",
"LOCATION": [
"imagekit_in_memory",
"imagekit_db",
],
},
"imagekit_in_memory": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "imagekit_in_memory_cache",
"TIMEOUT": None,
},
"imagekit_db": {
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
"LOCATION": "imagekit_cache",
"TIMEOUT": None,
},
}

# OpenSearch
Expand Down
1 change: 1 addition & 0 deletions scripts/run-django-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

python3 manage.py collectstatic --noinput --clear
python3 manage.py migrate --noinput
python3 manage.py createcachetable
RUN_DATA_MIGRATIONS=true python3 manage.py migrate --noinput

# load required fixtures on development by default
Expand Down

0 comments on commit 852067a

Please sign in to comment.