Skip to content

Instantly share code, notes, and snippets.

@vaibhav-jain
Last active February 20, 2016 07:18
Show Gist options
  • Save vaibhav-jain/dead5de06c64e9d0e049 to your computer and use it in GitHub Desktop.
Save vaibhav-jain/dead5de06c64e9d0e049 to your computer and use it in GitHub Desktop.
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