Last active
January 3, 2022 22:44
-
-
Save svetlyak40wt/11126018 to your computer and use it in GitHub Desktop.
Cache mixin for Django class based views.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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