Skip to content

Instantly share code, notes, and snippets.

@svetlyak40wt
Last active January 3, 2022 22:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svetlyak40wt/11126018 to your computer and use it in GitHub Desktop.
Save svetlyak40wt/11126018 to your computer and use it in GitHub Desktop.
Cache mixin for Django class based views.
class CachedMixin(object):
def get(self, *args, **kwargs):
cache_key, cache_ttl = self.get_cache_params(*args, **kwargs)
response = cache.get(cache_key)
if response is None:
response = super(CachedMixin, self).get(*args, **kwargs)
response.render()
cache.set(cache_key, response, cache_ttl)
return response
def get_cache_params(self, *args, **kwargs):
"""This method should return cache key and value TTL."""
raise NotImplementedError('Please, implement get_cache_params method.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment