# MongoDb Backend

class cachelib.mongodb.MongoDbCache(client=None, db='cache-db', collection='cache-collection', default_timeout=300, key_prefix=None, **kwargs)

  Bases: `BaseCache`

  Implementation of cachelib.BaseCache that uses mongodb collection as the backend.

  Limitations: maximum MongoDB document size is 16mb

  Parameters:

    - client (Any) – mongodb client or connection string

    - db (str | None) – mongodb database name

    - collection (str | None) – mongodb collection name

    - default_timeout (int) – Set the timeout in seconds after which cache entries expire

    - key_prefix (str | None) – A prefix that should be added to all keys.

    - kwargs (Any)

  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 | None) – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.

    Returns:

      Same as `set()`, but also `False` for already existing keys.

    Return type:

      boolean

  clear()

    Clears the cache. Keep in mind that not all caches support completely clearing the cache.

    Returns:

      Whether the cache has been cleared.

    Return type:

      boolean

  delete(key)

    Deletes an item from the cache. This is a no-op if the item doesn’t exist

    Parameters:

      key (str) – Key of the item to delete.

    Returns:

      True if the key existed and was deleted

    Return type:

      bool

  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

    Return type:

      boolean

  get(key)

    Get a cache item

    Parameters:

      key (str) – The cache key of the item to fetch

    Returns:

      cache value if not expired, else None

    Return type:

      Any

  get_dict(*keys)

    Like `get_many()` but return a dict:

        d = cache.get_dict("foo", "bar")
        foo = d["foo"]
        bar = d["bar"]

    Parameters:

      keys (str) – The function accepts multiple keys as positional arguments.

    Return type:

      Dict[str, Any]

  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()`.

    Parameters:

      keys (str) – The function accepts multiple keys as positional arguments.

    Return type:

      List[Any]

  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.

    Parameters:

      key (str) – the key to check

    Return type:

      bool

  serializer = <cachelib.serializers.BaseSerializer object>
  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 | None) – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.

    Returns:

      `True` if key has been updated, `False` for backend errors. Pickling errors, however, will raise a subclass of `pickle.PickleError`.

    Return type:

      boolean

  set_many(mapping, timeout=None)

    Sets multiple keys and values from a mapping.

    Parameters:

      - mapping (Dict[str, Any]) – a mapping with the keys/values to set.

      - timeout (int | None) – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.

    Returns:

      A list containing all keys successfully set

    Return type:

      boolean