Search code examples
djangodjango-modelsdjango-templatesdjango-cache

django cache query result for templates and views


I want to cache a query result on per request basis. Because I need that query in multiple places. And I want to cache that query on request and use it whenever and wherever I want. And at the end of the request-response cycle it should expire . Here I found a solution but there is a discussion stating it would be dangerous

Can someone please tell me how to achieve this.


Solution

  • You could modify the solution proposed here to use per-request locmemcache instances, instead of a global cache per thread.

    You could then use the middleware's process_response function to then cleanup the local cache.