Skip to content

Commit

Permalink
Merge pull request #11849 from rtibbles/naming_things_seems_easier
Browse files Browse the repository at this point in the history
Fix 500 errors when using django redis cache for response caching
  • Loading branch information
rtibbles authored Feb 9, 2024
2 parents dc97e82 + 7bd2a2e commit 32dd9a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions kolibri/core/content/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def wrapper_func(*args, **kwargs):
# Prevent the Django caching middleware from caching
# this response, as we want to cache it ourselves
request._cache_update_cache = False
key_prefix = get_cache_key(request)
key_prefix = cache_key_func(request)
url_key = hashlib.md5(
force_bytes(iri_to_uri(request.build_absolute_uri()))
).hexdigest()
Expand All @@ -129,7 +129,7 @@ def wrapper_func(*args, **kwargs):
response = view_func(*args, **kwargs)
if response.status_code == 200:
if key_prefix is None:
key_prefix = get_cache_key(request)
key_prefix = cache_key_func(request)
if (
key_prefix is not None
and hasattr(response, "render")
Expand Down
13 changes: 13 additions & 0 deletions kolibri/core/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.cache import caches
from django.core.cache import InvalidCacheBackendError
from django.utils.functional import SimpleLazyObject
from redis_cache import RedisCache as BaseRedisCache


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,3 +62,15 @@ def save(self):
logger.info("Overwriting Redis config")
self.client.config_rewrite()
self.changed = False


class RedisCache(BaseRedisCache):
def set(self, *args, **kwargs):
"""
Overwrite the set method to not return a value, in line with the Django cache interface
This causes particular issues for Django's caching middleware, which expects the set method to return None
as it invokes it directly in a lambda in the response.add_post_render_callback method
We use a similar pattern in our own caching decorator in kolibri/core/content/api.py and saw errors
due to the fact if the lambda returns a value, it is interpreted as a replacement for the response object.
"""
super(RedisCache, self).set(*args, **kwargs)
2 changes: 1 addition & 1 deletion kolibri/deployment/default/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

if cache_options["CACHE_BACKEND"] == "redis":
base_cache = {
"BACKEND": "redis_cache.RedisCache",
"BACKEND": "kolibri.core.utils.cache.RedisCache",
"LOCATION": cache_options["CACHE_LOCATION"],
# Default time out of each cache key
"TIMEOUT": cache_options["CACHE_TIMEOUT"],
Expand Down

0 comments on commit 32dd9a8

Please sign in to comment.