Skip to content

Commit

Permalink
Override the RedisCache set method to not return a value in line with…
Browse files Browse the repository at this point in the history
… Django's expectations of a cache backend.
  • Loading branch information
rtibbles committed Feb 8, 2024
1 parent 24ec3c4 commit 7bd2a2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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 7bd2a2e

Please sign in to comment.