Configuration¶
The following configuration values exist for Flask-Caching:
|
Specifies which type of caching object to use. This is an import string that will be imported and instantiated. It is assumed that the import object is a function that will return a cache object that adheres to the cache API. For flask_caching.backends.cache objects, you do not need to specify the entire import string, just one of the following names. Built-in cache types:
User contributed cache types:
|
|
Silence the warning message when using cache type of ‘NullCache’. |
|
Optional list to unpack and pass during the cache class instantiation. |
|
Optional dictionary to pass during the cache class instantiation. |
|
The timeout that is used if no other
timeout is specified. Unit of time is
seconds, a |
|
If set to |
|
The maximum number of items the cache
will store before it starts deleting
some. Used only for SimpleCache and
FileSystemCache. Defaults to |
|
A prefix that is added before all keys.
This makes it possible to use the same
memcached server for different apps.
Used only for RedisCache and MemcachedCache.
Defaults to |
|
The default condition applied to function
decorators which controls if the source code of
the function should be included when forming the
hash which is used as the cache key. This
ensures that if the source code changes, the
cached value will not be returned when the new
function is called even if the arguments are the
same. Defaults to |
|
hash_method used for hashing cache keys. Defaults to
|
|
The serializer the backend uses to serializer the values.
If |
|
Send Flask Signals for |
|
The name of the uwsgi caching instance to connect to, for example: mycache@localhost:3031, defaults to an empty string, which means uWSGI will cache in the local instance. If the cache is in the same instance as the werkzeug app, you only have to provide the name of the cache. |
|
The name of the Google Cloud Storage bucket to use. The bucket must already exist. Used only for GoogleCloudStorageCache. |
|
A list or a tuple of server addresses. Used only for MemcachedCache |
|
Username for SASL authentication with memcached. Used only for SASLMemcachedCache |
|
Password for SASL authentication with memcached. Used only for SASLMemcachedCache |
|
A Redis server host. Used only for RedisCache.
May also be an already created Redis client, see
Sharing a Redis client or connection pool.
Ignored if |
|
A Redis server port. Default is 6379.
Used only for RedisCache.
Ignored if |
|
A Redis password for server. Used only for RedisCache and
RedisSentinelCache.
Ignored if |
|
A Redis db (zero-based number index). Default is 0.
Used only for RedisCache and RedisSentinelCache.
Ignored if |
|
A list or a tuple of Redis sentinel addresses. Used only for RedisSentinelCache. |
|
The name of the master server in a sentinel configuration. Used only for RedisSentinelCache. |
|
A password for authenticating with the sentinel
servers themselves, as opposed to
|
|
A string of comma-separated Redis cluster node addresses. e.g. host1:port1,host2:port2,host3:port3 . Used only for RedisClusterCache. |
|
hash_method used for hashing the file names of cached
entries. Defaults to |
|
Directory to store cache. Used only for FileSystemCache. |
|
URL to connect to Redis server.
Example |
Flask-Caching will always use the CACHE_<BACKEND>_URL (if available) if a complete connection URI is provided.
For example, CACHE_REDIS_URL and the individual connection settings are two alternative
ways of describing the same connection. They are not merged: if CACHE_REDIS_URL is set,
the connection is built from the URL alone and CACHE_REDIS_HOST, CACHE_REDIS_PORT and
CACHE_REDIS_PASSWORD are silently ignored. This is intended behaviour, not a bug.
For example, this configuration connects without a password, because the URL contains
a database (/0):
config = {
"CACHE_TYPE": "RedisCache",
"CACHE_REDIS_URL": "redis://localhost:6379/0",
"CACHE_REDIS_PASSWORD": "hunter2", # ignored
}
Put the credentials in the URL instead:
config = {
"CACHE_TYPE": "RedisCache",
"CACHE_REDIS_URL": "redis://:hunter2@localhost:6379/0",
}
Or drop the URL and use the individual settings only:
config = {
"CACHE_TYPE": "RedisCache",
"CACHE_REDIS_HOST": "localhost",
"CACHE_REDIS_PORT": 6379,
"CACHE_REDIS_DB": 0,
"CACHE_REDIS_PASSWORD": "hunter2",
}
Using a cachelib backend directly¶
The built-in backends subclass their cachelib counterparts, so CACHE_TYPE
can also be an import string pointing straight at a cachelib class. This is the
way to use a backend that cachelib ships but Flask-Caching does not wrap, such
as MongoDbCache, DynamoDbCache or ValkeyCache.
For example, a MongoDbCache can look like this:
config = {
"CACHE_TYPE": "cachelib.MongoDbCache",
"CACHE_ARGS": ["mongodb://localhost:27017"],
"CACHE_OPTIONS": {"db": "myapp", "collection": "cache"},
}
and a ValkeyCache config like this:
config = {
"CACHE_TYPE": "cachelib.valkey.ValkeyCache",
"CACHE_OPTIONS": {
"host": "localhost",
"port": 6379,
"db": 0,
"key_prefix": "myapp",
},
}
The class is then instantiated directly: CACHE_ARGS is passed as positional
arguments and CACHE_OPTIONS as keyword arguments, together with
CACHE_DEFAULT_TIMEOUT. The other CACHE_* options (CACHE_DIR,
CACHE_THRESHOLD, CACHE_KEY_PREFIX, the CACHE_REDIS_* settings, …)
are only read by the built-in backends and are ignored here. Pass the
equivalent cachelib arguments through CACHE_OPTIONS instead.