Built-in Cache Backends

BaseCache

class BaseCache

Baseclass for all the cache backends listed below. It isn’t meant to be used directly, but you can subclass it to implement your own custom cache backend.

NullCache

class NullCache

Set CACHE_TYPE to NullCache to use this type.

Cache that doesn’t cache

  • CACHE_DEFAULT_TIMEOUT

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

SimpleCache

class SimpleCache

Set CACHE_TYPE to SimpleCache to use this type.

Uses a local python dictionary for caching. All operations are protected by a lock, making it thread-safe.

Relevant configuration values

  • CACHE_DEFAULT_TIMEOUT

  • CACHE_IGNORE_ERRORS

  • CACHE_THRESHOLD

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

FileSystemCache

class FileSystemCache

Set CACHE_TYPE to FileSystemCache to use this type.

Uses the filesystem to store cached values

  • CACHE_DEFAULT_TIMEOUT

  • CACHE_IGNORE_ERRORS

  • CACHE_DIR

  • CACHE_THRESHOLD

  • CACHE_OPTIONS

There is a single valid entry in CACHE_OPTIONS: mode, which should be a 3 digit linux-style permissions octal mode.

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

RedisCache

class RedisCache

Set CACHE_TYPE to RedisCache to use this type.

  • CACHE_DEFAULT_TIMEOUT

  • CACHE_IGNORE_ERRORS

  • CACHE_KEY_PREFIX

  • CACHE_OPTIONS

  • CACHE_REDIS_HOST

  • CACHE_REDIS_PORT

  • CACHE_REDIS_PASSWORD

  • CACHE_REDIS_DB

  • CACHE_REDIS_URL

Entries in CACHE_OPTIONS are passed to the redis client as **kwargs

CACHE_REDIS_HOST also accepts an already created Redis client instead of a host name. Use it to share one connection pool with the rest of the application, see Sharing a Redis client or connection pool.

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

RedisSentinelCache

class RedisSentinelCache

Set CACHE_TYPE to RedisSentinel to use this type.

  • CACHE_IGNORE_ERRORS

  • CACHE_KEY_PREFIX

  • CACHE_REDIS_SENTINELS

  • CACHE_REDIS_SENTINEL_MASTER

  • CACHE_REDIS_PASSWORD

  • CACHE_REDIS_SENTINEL_PASSWORD

  • CACHE_REDIS_DB

Entries in CACHE_OPTIONS are passed to the redis client as **kwargs

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

RedisClusterCache

class RedisClusterCache

Set CACHE_TYPE to RedisClusterCache to use this type.

  • CACHE_IGNORE_ERRORS

  • CACHE_KEY_PREFIX

  • CACHE_REDIS_CLUSTER

  • CACHE_REDIS_PASSWORD

  • CACHE_REDIS_URL

Entries in CACHE_OPTIONS are passed to the redis client as **kwargs

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

MemcachedCache

class MemcachedCache

Set CACHE_TYPE to MemcachedCache to use this type.

Uses a memcached server as a backend. Supports either pylibmc or memcache or google app engine memcache library.

Relevant configuration values

  • CACHE_DEFAULT_TIMEOUT

  • CACHE_IGNORE_ERRORS

  • CACHE_KEY_PREFIX

  • CACHE_MEMCACHED_SERVERS

  • CACHE_OPTIONS

MemcachedCache only accepts pool_size and pool_blocking in the CACHE_OPTIONS.

Changelog

Added in version 2.5.0: pool_size and pool_blocking options.

Note

Flask-Caching does not pass additional configuration options to memcached backends. To add additional configuration to these caches, directly set the configuration options on the object after instantiation:

from flask_caching import Cache
cache = Cache()

# Can't configure the client yet...
cache.init_app(flask_app, {"CACHE_TYPE": "MemcachedCache"})

# Break convention and set options on the _client object
# directly. For pylibmc behaviors:
cache.cache._client.behaviors({"tcp_nodelay": True})

Alternatively, see Custom Cache Backends.

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

SASLMemcachedCache

class SASLMemcachedCache

Set CACHE_TYPE to SASLMemcachedCache to use this type.

Uses a memcached server as a backend. Intended to be used with a SASL enabled connection to the memcached server. pylibmc is required and SASL must be supported by libmemcached.

Relevant configuration values

  • CACHE_DEFAULT_TIMEOUT

  • CACHE_IGNORE_ERRORS

  • CACHE_KEY_PREFIX

  • CACHE_OPTIONS

  • CACHE_MEMCACHED_SERVERS

  • CACHE_MEMCACHED_USERNAME

  • CACHE_MEMCACHED_PASSWORD

Note

Like MemcachedCache, pool_size/pool_blocking in CACHE_OPTIONS configure the connection pool. Unlike MemcachedCache, any other CACHE_OPTIONS entry is also accepted and forwarded as **kwargs to pylibmc.Client.

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

Changed in version 2.5.0: CACHE_IGNORE_ERRORS is now honoured by this backend.

Added in version 0.10.

SpreadSASLMemcachedCache

class SpreadSASLMemcachedCache

Set CACHE_TYPE to SpreadSASLMemcachedCache to use this type.

Same as SASLMemcachedCache however, it has the ability to spread value across multiple keys if it is bigger than the memcached threshold which by default is 1M. Uses pickle.

Changelog

Changed in version 2.5.0: Removed the deprecated old name in favour of just using the class name.

Changed in version 1.1.0: Renamed spreadsaslmemcachedcache to spreadsaslmemcached for the sake of consistency.

Added in version 0.11.