Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Region-based caching for Python/Flask with Redis

Project description

region_cache Documentation Status Updates

Region-based caching for Python/Flask with Redis


Region based Flask caching with Redis.

This module provides high-level nested, region-based caching with Redis. A region is a dot-separated namespace where cache keys and values live. It is implemented as hashes in redis. Parent-child relationships are implemented as sets. Timeouts use the EXPIRE command. All work is persisted in redis, and two regions with the same name on two different processes or even different servers will share the same storage.

Invalidation of caches is “active”, not “lazy”, so caches are purged immediately upon invalidation, to solve the problem of one process knowing about a cache invalidation and the other not. Cache writes are aggressively pipelined and transactional, so if two processes write to the same key at the same time, results will not be inconsistent.

The cache is written as a Flask extension, so you can use init_app with a valid flask app to initialize it. Simply set the CACHE_REDIS_URL setting in the config.


Using the region as a context manager treats everything in the context manager as a single transaction:

with region('') as r:
    x in r  # test for presence
    r[x] = 100  # get or KeyError
    x = r[x]  # set
    del r[x]  # remove

Bind to blinker signals, so the cache is purged declaratively:


Nest regions. If you invalidate the parent region, all the children will also be invalidated. This is recursive, so sub-sub-sub regions will be correctly invalidated as well:

region('abc').region('xyz')  # subregion
region('abc').invalidate()  # invalidate abc AND xyz

The default serializers is “pickle”, but you can supply any serializer that exposes a loads and dumps, and individual regions can be configured differently. Children inherit the settings of their parents.

Finally, timeouts are supported, and by default the timeout refreshes itself every time you write to the cache See the region() function for more detail on how to configure it.


This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.


0.1.0 (2018-05-04)

  • First release on PyPI.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for region-cache, version 0.3.1
Filename, size File type Python version Upload date Hashes
Filename, size region_cache-0.3.1-py2.py3-none-any.whl (10.3 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size region_cache-0.3.1.tar.gz (15.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page