A simple Django app that provides a per-request cache.
Project description
Django Request Cache provides a cache for each request (within your Django Request/Response cycle).
Quick start
Download and install using pip install
pip install django-request-cache
Add UserForeignKeyMiddleware and RequestCacheMiddleware to your MIDDLEWARE settings like this:
MIDDLEWARE = (
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
'django_userforeignkey.middleware.UserForeignKeyMiddleware',
'django_request_cache.middleware.RequestCacheMiddleware',
)
or if you are still using the an older Django version (e.g., Django 1.8) with MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
'django_userforeignkey.middleware.UserForeignKeyMiddleware',
'django_request_cache.middleware.RequestCacheMiddleware',
)
Use the per-request cache as a decorator
from django_request_cache import cache_for_request
@cache_for_request
def do_some_complex_calculation(a, b, c):
print("Calculating... please wait")
return a * b * c
Try it out by executing do_some_complex_calculation multiple times within your request
Attribution
RequestCache and RequestCacheMiddleware (see middleware.py) are from a source code snippet on StackOverflow https://stackoverflow.com/questions/3151469/per-request-cache-in-django/37015573#37015573 created by coredumperror https://stackoverflow.com/users/464318/coredumperror Original Question was posted by https://stackoverflow.com/users/7679/chase-seibert at https://stackoverflow.com/questions/3151469/per-request-cache-in-django copied on 2017-Dec-20
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-request-cache-1.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08a4e2312a33c0e30381825c10819e8fcfcf2bd3f126d24de038bc41ddd32fa5 |
|
MD5 | 3af4f26a837a03785d883f86c8e8f891 |
|
BLAKE2b-256 | f6498f5aac1eb92e4319cb143ffe6655f90aecf6517bd74a3fcdd43502532947 |
Hashes for django_request_cache-1.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f48750b81d53db636efb59450d320fde79be347786100be982c12b996f2d12f7 |
|
MD5 | 515dc1ddc9d5f103e8974c2744c61a1c |
|
BLAKE2b-256 | a3afbee79e9d9a641361d234329a5c1935812a2ba7d4087fb1e4e27d44e76de4 |