Last active
February 20, 2016 07:18
-
-
Save vaibhav-jain/dead5de06c64e9d0e049 to your computer and use it in GitHub Desktop.
Django RequestCacheMiddleware inspired by this SO post http://stackoverflow.com/questions/3151469/per-request-cache-in-django/8759188#8759188
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
from django.core.cache import caches | |
def get_request_cache(): | |
return caches['default'] | |
class RequestCacheMiddleware(object): | |
""" | |
Usage: | |
cache = get_request_cache() | |
cache.get('mykey') | |
""" | |
def process_request(self, request): | |
cache = get_request_cache() | |
cache.set('mykey', 'myvalue', timeout=None) | |
def process_response(self, request, response): | |
cache = get_request_cache() | |
cache.delete('mykey') | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment