MongoDb Backend¶
- class cachelib.mongodb.MongoDbCache(client=None, db='cache-db', collection='cache-collection', default_timeout=300, key_prefix=None, ignore_delete_many_errors=True, check_connection=False, **kwargs)¶
Bases:
BaseCacheImplementation of
BaseCachethat uses mongodb collection as the backend.Limitations: maximum
MongoDBdocument size is 16 MB- Parameters:
client (Any) – mongodb client or connection string
db (str) – mongodb database name
collection (str) – mongodb collection name
default_timeout (int | timedelta) –
Set the timeout after which cache entries expire, either a number of seconds or a
datetime.timedeltaChanged in version 0.17.0: Accepts a
datetime.timedelta.key_prefix (str | None) – A prefix that should be added to all keys.
ignore_delete_many_errors (bool) –
If False, delete_many() will raise a RuntimeError if any key fails to delete. Keys that do not exist are considered successfully deleted and do not raise.
Changelog
Added in version 0.16.0.
check_connection (bool) –
If True, the constructor will verify the connection to the mongodb server and raise a RuntimeError if it fails. If False (default), connection errors are ignored at construction and surface on first use.
Changelog
Added in version 0.16.1.
kwargs (Any)
- serializer = <cachelib.serializers.MongoDbSerializer object>¶
- get(key)¶
Get a cache item
- delete(key)¶
Deletes an item from the cache. This is a no-op if the item doesn’t exist
- set(key, value, timeout=None)¶
Add a new key/value to the cache (overwrites value, if key already exists in the cache).
- Parameters:
key (str) – the key to set
value (Any) – the value for the key
timeout (int | timedelta | None) – the cache timeout for the key, either a number of seconds or a
datetime.timedelta(if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.
- Returns:
Trueif key has been updated,Falsefor backend errors. Pickling errors, however, will raise a subclass ofpickle.PickleError.- Return type:
- set_many(mapping, timeout=None)¶
Sets multiple keys and values from a mapping.
- Parameters:
- Returns:
A list containing all keys successfully set
- Return type:
- get_many(*keys)¶
Returns a list of values for the given keys. For each key an item in the list is created:
foo, bar = cache.get_many("foo", "bar")
Has the same error handling as
get().
- get_dict(*keys)¶
Like
get_many()but return a dict:d = cache.get_dict("foo", "bar") foo = d["foo"] bar = d["bar"]
- add(key, value, timeout=None)¶
Works like
set()but does not overwrite the values of already existing keys.- Parameters:
key (str) – the key to set
value (Any) – the value for the key
timeout (int | timedelta | None) – the cache timeout for the key, either a number of seconds or a
datetime.timedelta(if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.
- Returns:
Same as
set(), but alsoFalsefor already existing keys.- Return type:
- has(key)¶
Checks if a key exists in the cache without returning it. This is a cheap operation that bypasses loading the actual data on the backend.
- delete_many(*keys)¶
Deletes multiple keys at once.
- Parameters:
keys (str) – The function accepts multiple keys as positional arguments.
- Returns:
A list containing all successfully deleted keys
- Raises:
RuntimeError – If
ignore_delete_many_errorsis False and a key still exists after the delete attempt.- Return type: