I would like to implement a decorator that provides per-request caching to any method, not just views. Here is an example use case.
I have a custom ta
This one uses a python dict as the cache (not the django's cache), and is dead simple and lightweight.
The same can be probably implemented with threadlocal storage. I am not aware of any downsides of this approach, feel free to add them in the comments.
from threading import currentThread
import weakref
_request_cache = weakref.WeakKeyDictionary()
def get_request_cache():
return _request_cache.setdefault(currentThread(), {})