A Redis cache backend for django

Overview

Redis Django Cache Backend

Downloads Latest Version Travis-ci Build

A Redis cache backend for Django

Docs can be found at http://django-redis-cache.readthedocs.org/en/latest/.

Changelog

3.0.0

  • Adds support for Python 3.8
  • Drops support for Python 2.X and Python 3.5
  • Drops support for Django < 3.0

2.1.2

  • Confirms support for Django 3.1 (no code changes required).

2.1.1

  • Fixes URL scheme for rediss://.

2.1.0

  • Adds support for Django 3.0.

2.0.0

  • Adds support for redis-py >= 3.0.
  • Drops support for Redis 2.6.
  • Drops support for Python 3.4.
  • Removes custom expire method in lieu of Django's touch.
  • Removes CacheKey in favor of string literals.
  • Adds testing for Django 2.2 and Python 3.7 (no code changes required).

1.8.0

  • Confirms support for Django 1.11, 2.0, and 2.1 (no code changes required).
  • Drops support for Django < 1.11.

1.7.1

  • Confirms support for Django 1.9 and 1.10.

1.7.0

  • Drops support for Django < 1.8 and Python 3.2.

1.6.4

  • Adds a default timeout to set_many.

1.6.3

  • Fixes get_many and set_many to work with empty parameters.

1.6.2

  • Fixes set_many to set cache key version.

1.6.1

  • Allows delete_many to fail silently with an empty list.

1.6.0

  • Adds dummy cache.

1.5.5

  • Cleans up get_or_set.

1.5.4

  • Updates importlib import statement for better Django 1.9 compatibility.

1.5.3

  • Adds initial documentation.
  • Updates function signatures to use DEFAULT_TIMEOUT.
  • Fixes issue with redis urls and unix_socket_path key error.

1.5.2

  • Adds SOCKET_CONNECT_TIMEOUT option.

1.5.1

  • Refactors class importing.

1.5.0

  • Adds ability to compress/decompress cache values using pluggable compressors

including zlib, bzip2, or a custom implementation.

1.4.0

  • Adds support for providing a socket timeout on the redis-py client.

1.3.0

  • Adds support for pluggable serializers including pickle(default), json,

msgpack, and yaml.

1.2.0

  • Deprecate support for Python 2.6. The cache should still work, but tests

will fail and compatibility will not be guaranteed going forward.

Backward incompatibilities:

  • The HashRing behavior has changed to maintain a proper keyspace balance.

This will lead to some cache misses, so be aware.

1.0.0

  • Deprecate support for django < 1.3 and redis < 2.4. If you need support for those versions,
    pin django-redis-cache to a version less than 1.0, i.e. pip install django-redis-cache<1.0
  • Application level sharding when a list of locations is provided in the settings.
  • Delete keys using wildcard syntax.
  • Clear cache using version to delete only keys under that namespace.
  • Ability to select pickle protocol version.
  • Support for Master-Slave setup
  • Thundering herd protection
  • Add expiration to key using expire command.
  • Add persistence to key using persist command.

0.13.0

  • Adds custom has_key implementation that uses Redis's exists command.
    This will speed has_key up drastically if the key under question is extremely large.

0.12.0

  • Keys can now be kept alive indefinitely by setting the timeout to None,
    e.g. cache.set('key', 'value', timeout=None)
  • Adds ttl method to the cache. cache.ttl(key) will return the number of
    seconds before it expires or None if the key is not volatile.

0.11.0

  • Adds support for specifying the connection pool class.
  • Adds ability to set the max connections for the connection pool.

0.10.0

Adds Support for Python 3.3 and Django 1.5 and 1.6. Huge thanks to Carl Meyer for his work.

0.9.0

Redis cache now allows you to use either a TCP connection or Unix domain socket to connect to your redis server. Using a TCP connection is useful for when you have your redis server separate from your app server and/or within a distributed environment. Unix domain sockets are useful if you have your redis server and application running on the same machine and want the fastest possible connection.

You can now specify (optionally) what parser class you want redis-py to use when parsing messages from the redis server. redis-py will pick the best parser for you implicitly, but using the PARSER_CLASS setting gives you control and the option to roll your own parser class if you are so bold.

Requirements

redis-py >= 2.10.3 redis >= 2.4 hiredis python >= 2.7

  1. Run pip install django-redis-cache.
  2. Modify your Django settings to use redis_cache.
# When using TCP connections
CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': [
            '<host>:<port>',
            '<host>:<port>',
            '<host>:<port>',
        ],
        'OPTIONS': {
            'DB': 1,
            'PASSWORD': 'yadayada',
            'PARSER_CLASS': 'redis.connection.HiredisParser',
            'CONNECTION_POOL_CLASS': 'redis.BlockingConnectionPool',
            'CONNECTION_POOL_CLASS_KWARGS': {
                'max_connections': 50,
                'timeout': 20,
            },
            'MAX_CONNECTIONS': 1000,
            'PICKLE_VERSION': -1,
        },
    },
}

# When using unix domain sockets
# Note: ``LOCATION`` needs to be the same as the ``unixsocket`` setting
# in your redis.conf
CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': '/path/to/socket/file',
        'OPTIONS': {
            'DB': 1,
            'PASSWORD': 'yadayada',
            'PARSER_CLASS': 'redis.connection.HiredisParser',
            'PICKLE_VERSION': 2,
        },
    },
}

# For Master-Slave Setup, specify the host:port of the master
# redis-server instance.
CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': [
            '<host>:<port>',
            '<host>:<port>',
            '<host>:<port>',
        ],
        'OPTIONS': {
            'DB': 1,
            'PASSWORD': 'yadayada',
            'PARSER_CLASS': 'redis.connection.HiredisParser',
            'PICKLE_VERSION': 2,
            'MASTER_CACHE': '<master host>:<master port>',
        },
    },
}

Usage

django-redis-cache shares the same API as django's built-in cache backends, with a few exceptions.

cache.delete_pattern

Delete keys using glob-style pattern.

example:

>>> from news.models import Story
>>>
>>> most_viewed = Story.objects.most_viewed()
>>> highest_rated = Story.objects.highest_rated()
>>> cache.set('news.stories.most_viewed', most_viewed)
>>> cache.set('news.stories.highest_rated', highest_rated)
>>> data = cache.get_many(['news.stories.highest_rated', 'news.stories.most_viewed'])
>>> len(data)
2
>>> cache.delete_pattern('news.stores.*')
>>> data = cache.get_many(['news.stories.highest_rated', 'news.stories.most_viewed'])
>>> len(data)
0

cache.clear

Same as django's cache.clear, except that you can optionally specify a version and all keys with that version will be deleted. If no version is provided, all keys are flushed from the cache.

cache.reinsert_keys

This helper method retrieves all keys and inserts them back into the cache. This is useful when changing the pickle protocol number of all the cache entries. As of django-redis-cache < 1.0, all cache entries were pickled using version 0. To reduce the memory footprint of the redis-server, simply run this method to upgrade cache entries to the latest protocol.

Thundering Herd Protection

A common problem with caching is that you can sometimes get into a situation where you have a value that takes a long time to compute or retrieve, but have clients accessing it a lot. For example, if you wanted to retrieve the latest tweets from the twitter api, you probably want to cache the response for a number of minutes so you don't exceed your rate limit. However, when the cache entry expires you can have mulitple clients that see there is no entry and try to simultaneously fetch the latest results from the api.

The way to get around this problem you pass in a callable and timeout to get_or_set, which will check the cache to see if you need to compute the value. If it does, then the cache sets a placeholder that tells future clients to serve data from the stale cache until the new value is created.

Example:

tweets = cache.get_or_set('tweets', twitter.get_newest, timeout=300)

Running Tests

./install_redis.sh

make test

This is a Django app that uses numerous Google APIs such as reCAPTURE, maps and waypoints

Django project that uses Googles APIs to auto populate fields, display maps and routes for multiple waypoints

Bobby Stearman 57 Dec 03, 2022
PEP-484 stubs for Django

pep484 stubs for Django This package contains type stubs and a custom mypy plugin to provide more precise static types and type inference for Django f

TypedDjango 1.1k Dec 30, 2022
django-compat-lint

django_compat_lint -- check Django compatibility of your code Django's API stability policy is nice, but there are still things that change from one v

James Bennett 40 Sep 30, 2021
Dynamic Django settings.

Constance - Dynamic Django settings A Django app for storing dynamic settings in pluggable backends (Redis and Django model backend built in) with an

Jazzband 1.5k Jan 07, 2023
Auto-detecting the n+1 queries problem in Python

nplusone nplusone is a library for detecting the n+1 queries problem in Python ORMs, including SQLAlchemy, Peewee, and the Django ORM. The Problem Man

Joshua Carp 837 Dec 29, 2022
πŸ“ŠπŸ“ˆ Serves up Pandas dataframes via the Django REST Framework for use in client-side (i.e. d3.js) visualizations and offline analysis (e.g. Excel)

Django REST Pandas Django REST Framework + pandas = A Model-driven Visualization API Django REST Pandas (DRP) provides a simple way to generate and se

wq framework 1.2k Jan 01, 2023
django-tables2 - An app for creating HTML tables

django-tables2 - An app for creating HTML tables django-tables2 simplifies the task of turning sets of data into HTML tables. It has native support fo

Jan Pieter Waagmeester 1.6k Jan 03, 2023
This is django-import-export module that exports data into many formats

django-import-export This is django-import-export module which exports data into many formats, you can implement this in your admin panel. - Dehydrat

Shivam Rohilla 3 Jun 03, 2021
A Django application that provides country choices for use with forms, flag icons static files, and a country field for models.

Django Countries A Django application that provides country choices for use with forms, flag icons static files, and a country field for models. Insta

Chris Beaven 1.2k Jan 07, 2023
Redia Cache implementation in django.

django-redis Recipe APP Simple Recipe app which shows different kinds off recipe to the user. Why Cache ? Accessing data from cache is much faster tha

Avinash Alanjkar 1 Sep 21, 2022
Utilities to make function-based views cleaner, more efficient, and better tasting.

django-fbv Utilities to make Django function-based views cleaner, more efficient, and better tasting. πŸ’₯ πŸ“– Complete documentation: https://django-fbv

Adam Hill 49 Dec 30, 2022
django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project. Inspired in the dashboard framework Dashing

django-dashing django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project.

talPor Solutions 703 Dec 22, 2022
A simple Django middleware for Duo V4 2-factor authentication.

django-duo-universal-auth A lightweight middleware application that adds a layer on top of any number of existing authentication backends, enabling 2F

Adam Angle 1 Jan 10, 2022
Helps working with singletons - things like global settings that you want to edit from the admin site.

Django Solo +---------------------------+ | | | | | \ | Django Solo helps

Sylvain ToΓ© 726 Jan 08, 2023
E-Commerce Platform

Shuup Shuup is an Open Source E-Commerce Platform based on Django and Python. https://shuup.com/ Copyright Copyright (c) 2012-2021 by Shuup Commerce I

Shuup 2k Jan 07, 2023
A Django app that creates automatic web UIs for Python scripts.

Wooey is a simple web interface to run command line Python scripts. Think of it as an easy way to get your scripts up on the web for routine data anal

Wooey 1.9k Jan 08, 2023
A simple demonstration of how a django-based website can be set up for local development with microk8s

Django with MicroK8s Start Building Your Project This project provides a Django web app running as a single node Kubernetes cluster in microk8s. It is

Noah Jacobson 19 Oct 22, 2022
A tool to automatically fix Django deprecations.

A tool to help upgrade Django projects to newer version of the framework by automatically fixing deprecations. The problem When maintaining a Django s

Bruno Alla 155 Dec 14, 2022
Service request portal on top of Ansible Tower

Squest - A service request portal based on Ansible Tower Squest is a Web portal that allow to expose Tower based automation as a service. If you want

Hewlett Packard Enterprise 183 Jan 04, 2023
PWA is a simple Django app to develope and deploy a Progressive Web Application.

PWA PWA is a simple Django app to develope and deploy a Progressive Web Application. Detailed documentation is in the "docs" directory. Quick start Ad

Nima 6 Dec 09, 2022