Skip to main content

A simple Django app that provides a per-request cache.

Project description

https://badge.fury.io/py/django-request-cache.svg https://github.com/anexia/django-request-cache/actions/workflows/test.yml/badge.svg?branch=main https://codecov.io/gh/anexia/django-request-cache/branch/main/graph/badge.svg

Django Request Cache provides a cache for each request (within your Django Request/Response cycle).

Quick start

  1. Download and install using pip install

pip install django-request-cache
  1. 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',
)
  1. 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

django-request-cache-1.5.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

django_request_cache-1.5.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file django-request-cache-1.5.0.tar.gz.

File metadata

  • Download URL: django-request-cache-1.5.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.18

File hashes

Hashes for django-request-cache-1.5.0.tar.gz
Algorithm Hash digest
SHA256 d4659cdc21afececa7e9f9c36afd252dfb189b43bc1af0b1916294deff7868c6
MD5 81781e8dbca37b91287d704a6d8d0f32
BLAKE2b-256 e008a06444b95cd9fcd637f54a6a35e5c6e878cec0fd4b05b9287486dc10dbce

See more details on using hashes here.

File details

Details for the file django_request_cache-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_request_cache-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31bf8109d260310e144f28f502f16ae0c1ab9bb6bd6ae3b7429cc3f587bb0bd8
MD5 2f3d59dc8f6c7a384d6aa48f797a8aaa
BLAKE2b-256 f10f43e799a7524be92137445b13b304b78908eb4bc8eeef65adeb2204a91eeb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page