django-cachalot

2.9.0 · active · verified Thu Apr 16

django-cachalot is a Django package that automatically caches all Django ORM queries and transparently invalidates them when data changes. It aims to provide significant performance improvements by reducing database hits without requiring developers to write explicit caching logic. The library is actively maintained, with version 2.9.0 supporting recent Django releases and Python versions, and new releases frequently adapting to Django's evolution.

Common errors

Warnings

Install

Imports

Quickstart

To enable django-cachalot, add 'cachalot' to your `INSTALLED_APPS` and ensure you have a Django cache backend configured in your `CACHES` setting. For production, a shared cache like Redis or Memcached is recommended. For local development, `LocMemCache` can be used, but be aware of its limitations in multi-process environments. It's often beneficial to configure `IGNORE_EXCEPTIONS` for your cache backend to prevent application crashes if the cache service is unavailable.

import os

# settings.py

SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'a-very-secret-key')

INSTALLED_APPS = [
    # ... other apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cachalot',
]

# Configure a cache backend (e.g., Redis)
CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379/1'),
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
            'IGNORE_EXCEPTIONS': True # Recommended for graceful degradation if cache is down
        }
    }
}

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

# To verify caching, you can add CachalotPanel to Django Debug Toolbar
DEBUG_TOOLBAR_PANELS = [
    # ... other panels
    'cachalot.panels.CachalotPanel',
]

view raw JSON →