Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anexia/django-request-cache
A Django app that provides a new cache on every request object. The cache is only kept within the request/response cycle.
https://github.com/anexia/django-request-cache
django hacktoberfest python
Last synced: 19 days ago
JSON representation
A Django app that provides a new cache on every request object. The cache is only kept within the request/response cycle.
- Host: GitHub
- URL: https://github.com/anexia/django-request-cache
- Owner: anexia
- License: mit
- Created: 2017-12-20T13:15:18.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2024-11-06T15:16:19.000Z (about 1 month ago)
- Last Synced: 2024-11-13T18:43:32.109Z (30 days ago)
- Topics: django, hacktoberfest, python
- Language: Python
- Homepage:
- Size: 27.3 KB
- Stars: 28
- Watchers: 4
- Forks: 14
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-django-performance - django-request-cache - A Django app that provides a new cache on every request object. The cache is only kept within the request/response cycle. (Caching / Tools)
README
====================
Django Request Cache
====================.. image:: https://badge.fury.io/py/django-request-cache.svg
:target: https://badge.fury.io/py/django-request-cache
.. image:: https://github.com/anexia/django-request-cache/actions/workflows/test.yml/badge.svg?branch=main
:target: https://github.com/anexia/django-request-cache/actions/workflows/test.yml
.. image:: https://codecov.io/gh/anexia/django-request-cache/branch/main/graph/badge.svg
:target: https://codecov.io/gh/anexia/django-request-cacheDjango Request Cache provides a cache for each request (within your Django Request/Response cycle).
Quick start
-----------1. Download and install using `pip install`
.. code-block:: bash
pip install django-request-cache
2. Add ``UserForeignKeyMiddleware`` and ``RequestCacheMiddleware`` to your ``MIDDLEWARE`` settings like this:
.. code-block:: python
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``:
.. code-block:: python
MIDDLEWARE_CLASSES = (
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
'django_userforeignkey.middleware.UserForeignKeyMiddleware',
'django_request_cache.middleware.RequestCacheMiddleware',
)3. Use the per-request cache as a decorator
.. code-block:: python
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 * cTry 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