Caching

Libraries for caching data.

Expensive function calls get cached in memory by one cachetools decorator. DiskCache stores cached data on local disk, with no server to run.

How to choose:

  • Memoizing a function in one process, with a size limit or an expiry time: cachetools
  • A persistent cache on local disk that several processes share: DiskCache
  • A Django cache backend on local disk: DiskCache
  • A shared Redis or Memcached cache outside Django, with coordinated value regeneration: dogpile.cache
  • A Django cache and session store on Redis: django-redis
  • Caching HTTP responses in HTTPX or Requests, or setting cache headers in FastAPI: Hishel
  • Django ORM queries cached in Redis and invalidated when the data changes: Cacheops

Listed in editorial order, grouped by use case. Click a column header to re-sort the whole list.

Press / to search. Tags open their category. Click a row for details.

Search and filter

Results

Row number Tags

General 3 projects

Extensible memoizing collections and decorators.
tkem/github.com/tkem/cachetools /2,789 stars /
SQLite and file-backed cache backend, compatible with Django.
A caching API for Redis, Memcached, and others, with coordinated value regeneration.

HTTP Responses 1 project

RFC 9111-compliant HTTP caching for clients like httpx and requests and servers like FastAPI, with sync and async support.

Django 2 projects

Full-featured Redis cache backend for Django.
jazzband/github.com/jazzband/django-redis /3,086 stars /
A slick ORM cache with automatic granular event-driven invalidation.
Suor/github.com/Suor/django-cacheops /2,271 stars /

Caching guide

cachetools offers variants of the standard library's @lru_cache decorator. Its cache classes have a maximum size and drop items by different rules, like least recently used or a per-item time-to-live. Wrap a function in @cached with one of them: the README's @cached(cache=TTLCache(maxsize=1024, ttl=600)) keeps results for ten minutes. To keep the lru_cache API instead, use the cachetools.func decorators.

DiskCache keeps keys and values in a SQLite database and a directory of files, with no server process to run. Point a Cache at a directory, and other threads and processes that open the same directory share its entries. Its memoize() decorator takes arguments like lru_cache's, plus an expiry time. In Django, set diskcache.DjangoCache as the BACKEND and a directory as the LOCATION.

dogpile.cache gives you one caching API over Redis, Memcached, and other backends, without depending on a web framework. Create a region with make_region(), configure its backend, and call get_or_create() with a key and a function that computes the value. For shared Redis or Memcached caches, enable distributed_lock so one worker regenerates an expired value while others use the previous one.

django-redis is a full-featured Redis cache and session backend for Django. Set django_redis.cache.RedisCache as the BACKEND and a redis-py URL like redis://127.0.0.1:6379/1 as the LOCATION. Sessions then need no extra backend: point SESSION_ENGINE at Django's cache sessions, and they live in Redis too. Its clients, parsers, and serializers are pluggable.

Hishel caches HTTP responses by RFC 9111, the rules browsers follow. It works in sync and async code, and comes with a backend that stores responses in SQLite. In HTTPX, use SyncCacheClient or AsyncCacheClient, drop-in replacements for Client and AsyncClient. In Requests, mount its CacheAdapter on a Session. In FastAPI, the cache() dependency sends Cache-Control headers to browsers and CDNs, and Hishel's ASGI middleware adds a cache on your server.

Cacheops caches Django ORM queries in Redis and invalidates them on save(), delete(), and m2m changes, only where a change could affect the result. Add cacheops to INSTALLED_APPS, then set CACHEOPS_REDIS and a CACHEOPS profile per app like 'app_name.*'. Profiles with ops cache those queries automatically. To cache by hand, call .cache() on a queryset, or decorate a function with @cached_as, and it still invalidates with the model.

Know a project that belongs here?

Tell us what it does and why it stands out.