django-cachalot

Automatically caches and invalidates Django ORM queries with zero code changes

Library
PyPI
v2.9.1
1,427stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
58/100Fair
Development Activity48
Maintenance24
Community72
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
72/100Good
Architecture72
Code Quality70
Innovation65
Learning Curve80

django-cachalot is a Django app that transparently caches every read query made through the Django ORM and automatically invalidates the cache whenever the underlying tables change. Instead of manually wrapping views or querysets in cache logic, teams add cachalot to INSTALLED_APPS and get application-wide ORM caching for free.

It supports PostgreSQL, SQLite, and MySQL, and plugs into any Django cache backend (Memcached, Redis, etc.) via Django’s standard cache framework. Cache invalidation is table-aware: writes to a table invalidate exactly the cached queries that touched it, avoiding both stale reads and full-cache flushes.

What You Get

  • Automatic caching of all Django ORM read queries with no per-view or per-queryset code changes
  • Table-aware automatic invalidation on INSERT/UPDATE/DELETE, avoiding stale reads
  • Support for PostgreSQL, SQLite, and MySQL across a wide range of Django versions
  • A manage.py command to manually invalidate the cache after out-of-band data changes (e.g. SQL restores)
  • Optional Django Debug Toolbar panel (CachalotPanel) for inspecting cache behavior
  • Jinja2 template extension support alongside the standard Django template layer

Common Use Cases

  • Speeding up read-heavy Django views and APIs without writing manual caching logic
  • Reducing database load on high-traffic Django sites backed by Postgres/MySQL
  • Adding a safety net of query caching to existing apps where introducing per-view cache invalidation logic would be error-prone
  • Benchmarking and comparing ORM-level caching against manual view/fragment caching strategies

Under The Hood

Architecture: django-cachalot works by monkey-patching Django’s SQL compiler (monkey_patch.py) so that every read query is intercepted, hashed, and looked up in the configured Django cache backend before hitting the database; signals.py listens for post-save/post-delete/schema-change signals to invalidate the relevant table’s cached entries, and transaction.py coordinates invalidation timing with Django’s transaction lifecycle so cache writes don’t leak uncommitted data. api.py exposes manual cache-control helpers (e.g. force-invalidate) for edge cases like out-of-band SQL restores. Tech Stack: Pure Python, packaged as a standard Django app (cachalot in INSTALLED_APPS), with no required third-party dependencies beyond Django itself — it relies entirely on Django’s built-in cache framework, so it works with whatever backend (Memcached, Redis, LocMem) the host project already configures. CI runs the test suite via tox across Python 3.8-3.14 and Django 4.2/5.2/6.0 against PostgreSQL, SQLite, and MySQL. Code Quality: The core cachalot package is compact (~1,200 lines) with a dedicated tests/ and admin_tests/ suite, plus coverage tracked via Coveralls and static analysis via Scrutinizer CI, indicating a disciplined test/CI setup for a project of its size. API Design: The integration surface is minimal by design — install and add to INSTALLED_APPS — with an explicit warning in the docs about upper Django-version bounds for safety, and a documented limits.html page covering known caveats (e.g. multi-server clock sync) rather than hiding them, which is a strong signal of an honest, low-boilerplate developer experience.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search