Skip to main content

django-in-request-cache

Django cache backend stored in django request

https://travis-ci.org/mojeto/django-in-request-cache.svg?branch=master&maxAge=259200 https://img.shields.io/pypi/v/django-in-request-cache.svg?maxAge=259200 https://img.shields.io/pypi/l/django-in-request-cache.svg?maxAge=2592000

Installation

$ pip install django-in-request-cache

Fast InRequestCache

InRequestCache is implementation of django cache interface. It uses instance of python dict assigned to each django request object to store cached values. django-globals is used to make request object accessible everywhere. This cache has the same limitations as standard django InMemory cache - Cross process access isn’t possible. InRequestCache goes one step further and makes cross thread access impossible. InRequestCache is different in each request. InRequestCache should be faster than InMemory cache, because there is no read/write lock. It makes sense to use InRequestCache for values which will be accessible multiple times in the same request. Cache invalidation is hard, but because cache lives for request period only it isn’t a big problem.

Quick start

  1. Add “django-in-request-cache” to your setting as cache like this:

    from django.conf.global_settings import CACHES as DEFAULT_CACHES
    
    CACHES = dict(
        DEFAULT_CACHES,
        cache_in_request={
            'BACKEND': 'django_in_request_cache.cache.InRequestCache',
            # 'LOCATION': '_dinr_cache',  # request property name to store data
            # 'OPTIONS': {
            #     # if set then no value is stored for more than MAX_TIME time.
            #     'MAX_TIMEOUT': 10,  # in seconds,
            # },
        },
    )
  2. Add django-globals middleware to your settings like this:

    MIDDLEWARE_CLASSES = [
        ...,
        'django_globals.middleware.Global',
    ]

Speed up slower cache with faster cache

Why do we want to cache a cache? In my case I have one value in redis cache, which was accessed 20 times during the same django request. Every read from redis takes ~1ms, it makes ~20ms just read the same value 20 times. To speed it up I want cache my value in faster cache (InRequestCache, InMemoryCache etc.) CacheACache class is implementation of django cache interface which allows read value from slower cache only once and ‘cache’ it again in faster in memory cache. Most of the time faster cache is back populated from slower cache. In this case we doesn’t have information whe value expire. In that case cache max expiration time for cached value is value expire time + slow cache expiration time. Therefore fast cache expiration time should be set very low (in number of seconds).

CacheACache configuration

  • How to cache a slower but cross process cache backend:

    from django.conf.global_settings import CACHES as DEFAULT_CACHES
    
    CACHES = dict(
        DEFAULT_CACHES,
        redis_cache = {
            'BACKEND: 'redis_cache.RedisCache',
            ...
        },
        cache_in_request={
            'BACKEND': 'django_in_request_cache.cache.InRequestCache',
            'LOCATION': '_redis_cache_mirror',  # request property name
        },
        combined_in_request_and_redis_cache={
            'BACKEND': 'django_in_request_cache.cache.CacheACache',
            'OPTIONS: {
                'FAST_CACHE': 'cache_in_request',  # cache alias
                'FAST_CACHE_MAX_TIMEOUT': 5,  # in seconds
                'CACHE_TO_CACHE': 'redis_cache',  # cache alias
            },
        },
    )

Requirements

License

Metadata

Release files for django-in-request-cache 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-in-request-cache 0.0.6
File Size Uploaded Provenance Actions
django-in-request-cache-0.0.6.tar.gz 5.8 kB Without provenance Details for django-in-request-cache-0.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-in-request-cache 0.0.6
File Interpreter ABI Platform Actions
django_in_request_cache-0.0.6-py2.py3-none-any.whl Python 3, Python 2 none any Details for django_in_request_cache-0.0.6-py2.py3-none-any.whl

Total release size: 14.4 kB

Release files / django-in-request-cache-0.0.6.tar.gz

Download URL django-in-request-cache-0.0.6.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
447c0c8278aa88c9c9e9a8ff86b4d2d277b78b81a3b2bd7bd91e3d6418968aac
BLAKE2b-256 checksum
How to use checksums
c0a9b5c90e5182239709db06a8fa9f674a55ad9059404cd1bad891636e08da36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_in_request_cache-0.0.6-py2.py3-none-any.whl

Download URL django_in_request_cache-0.0.6-py2.py3-none-any.whl
Size 8.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8f9ac541cbc5a7efbe59a8ec265d139bd0ad5d36a8efbf6a739c02041ea6f3c6
BLAKE2b-256 checksum
How to use checksums
cfd75887c077ef7150026a3a574cb6049900ac2403722e7308c446cd3a89d148
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-in-request-cache 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page