Skip to content
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

Fix 500 errors when using django redis cache for response caching #11849

Merged

Conversation

rtibbles
Copy link
Member

@rtibbles rtibbles commented Feb 8, 2024

Summary

  • Fixes a small oversight whereby the passed in caching key generation function is not used consistently in the function decorator (not directly related to this fix, but important cleanup)
  • Django Redis Cache does not implement an idiomatic cache backend for Django, inasmuch as it always returns a value for each of its cache methods.
  • This causes particular issues both in our code and in the code used by the default Django cache middleware, whereby a response is cached in its post render callback, with the implementation using a lambda to invoke the cache setting, with the expectation that cache.set returns None.
  • Due to the fact that the django-redis-cache implementation returns the response from the Redis client as the return of the set function (in the case of a successful set ' True) this is returned from the lambda and assumed by Django to be a replacement response object to be returned instead of the original. Hilarity ensues.
  • This is fixed by subclassing the django-redis-cache RedisCache class and simply not returning anything from the set function while invoking the super method.

References

Fixes #11848

Reviewer guidance

Load an uncached contentnode API endpoint and confirm it does not return a 500.


Testing checklist

  • Contributor has fully tested the PR manually
  • If there are any front-end changes, before/after screenshots are included
  • Critical user journeys are covered by Gherkin stories
  • Critical and brittle code paths are covered by unit tests

PR process

  • PR has the correct target branch and milestone
  • PR has 'needs review' or 'work-in-progress' label
  • If PR is ready for review, a reviewer has been added. (Don't use 'Assignees')
  • If this is an important user-facing change, PR or related issue has a 'changelog' label
  • If this includes an internal dependency change, a link to the diff is provided

Reviewer checklist

  • Automated test coverage is satisfactory
  • PR is fully functional
  • PR has been tested for accessibility regressions
  • External dependency files were updated if necessary (yarn and pip)
  • Documentation is updated
  • Contributor is in AUTHORS.md

@rtibbles rtibbles requested a review from jredrejo February 8, 2024 22:38
@github-actions github-actions bot added DEV: backend Python, databases, networking, filesystem... SIZE: small labels Feb 8, 2024
@rtibbles rtibbles added the TODO: needs review Waiting for review label Feb 8, 2024
Copy link
Member

@jredrejo jredrejo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested in a RPi, it fixed the issue and everything worked perfectly.
Code looks good too.

class RedisCache(BaseRedisCache):
def set(self, *args, **kwargs):
"""
Overwrite the set method to not return a value, in line with the Django cache interface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant!

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surprised by this... until I re-read the PR description

@rtibbles rtibbles merged commit 32dd9a8 into learningequality:release-v0.16.x Feb 9, 2024
34 checks passed
@rtibbles rtibbles deleted the naming_things_seems_easier branch February 9, 2024 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DEV: backend Python, databases, networking, filesystem... SIZE: small TODO: needs review Waiting for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants