MongoDB

../_images/mongodb.png

MongoDB is a NoSQL document database. It stores data in collections of documents, which are more flexible and less strictly structured than tables in a relational database.

Connection Options

The MongoDB backend accepts any keyword arguments for pymongo.mongo_client.MongoClient. These can be passed via CachedSession:

>>> session = CachedSession('http_cache', backend='mongodb', host='192.168.1.63', port=27017)

Or via MongoCache:

>>> backend = MongoCache(host='192.168.1.63', port=27017)
>>> session = CachedSession('http_cache', backend=backend)

API Reference

MongoCache

MongoDB cache backend

MongoDict

A dictionary-like interface for a MongoDB collection

MongoPickleDict

Same as MongoDict, but pickles values before saving

class requests_cache.backends.mongodb.MongoCache(db_name='http_cache', connection=None, **kwargs)[source]

Bases: requests_cache.backends.base.BaseCache

MongoDB cache backend

Parameters
bulk_delete(keys)

Remove multiple responses and their associated redirects from the cache

Parameters

keys (Iterable[str]) –

clear()

Delete all items from the cache

create_key(request=None, **kwargs)

Create a normalized cache key from a request object

Parameters

request (Union[PreparedRequest, CachedRequest, None]) –

Return type

str

delete(key)

Delete a response or redirect from the cache, as well any associated redirect history

Parameters

key (str) –

delete_url(url, method='GET', **kwargs)

Delete a cached response for the specified request

Parameters
  • url (str) –

  • method (str) –

delete_urls(urls, method='GET', **kwargs)

Delete all cached responses for the specified requests

Parameters
get_response(key, default=None)

Retrieve a response from the cache, if it exists

Parameters
  • key (str) – Cache key for the response

  • default – Value to return if key is not in the cache

Return type

CachedResponse

has_key(key)

Returns True if key is in the cache

Parameters

key (str) –

Return type

bool

has_url(url, method='GET', **kwargs)

Returns True if the specified request is cached

Parameters
  • url (str) –

  • method (str) –

Return type

bool

keys(check_expiry=False)

Get all cache keys for redirects and valid responses combined

Return type

Iterator[str]

remove_expired_responses(expire_after=None)

Remove expired and invalid responses from the cache, optionally with revalidation

Parameters

expire_after (Union[None, int, float, str, datetime, timedelta]) – A new expiration time used to revalidate the cache

response_count(check_expiry=False)

Get the number of responses in the cache, excluding invalid (unusable) responses. Can also optionally exclude expired responses.

Return type

int

save_response(response, cache_key=None, expires=None)

Save a response to the cache

Parameters
  • cache_key (Optional[str]) – Cache key for this response; will otherwise be generated based on request

  • response (Union[Response, CachedResponse]) – response to save

  • expire_after – Time in seconds until this cache item should expire

  • expires (Optional[datetime]) –

update(other)

Update this cache with the contents of another cache

Parameters

other (BaseCache) –

property urls

Get all URLs currently in the cache (excluding redirects)

Return type

Iterator[str]

values(check_expiry=False)

Get all valid response objects from the cache

Return type

Iterator[CachedResponse]

class requests_cache.backends.mongodb.MongoDict(db_name, collection_name='http_cache', connection=None, **kwargs)[source]

Bases: requests_cache.backends.base.BaseStorage

A dictionary-like interface for a MongoDB collection

Parameters
  • db_name – Database name

  • collection_name – Collection name

  • connectionpymongo.MongoClient object to reuse instead of creating a new one

  • kwargs – Additional keyword arguments for pymongo.MongoClient

bulk_delete(keys)[source]

Delete multiple keys from the cache. Does not raise errors for missing keys.

Parameters

keys (Iterable[str]) –

clear() None.  Remove all items from D.[source]
get(k[, d]) D[k] if k in D, else d.  d defaults to None.
items() a set-like object providing a view on D’s items
keys() a set-like object providing a view on D’s keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D’s values
class requests_cache.backends.mongodb.MongoPickleDict(db_name, collection_name='http_cache', connection=None, **kwargs)[source]

Bases: requests_cache.backends.mongodb.MongoDict

Same as MongoDict, but pickles values before saving

bulk_delete(keys)

Delete multiple keys from the cache. Does not raise errors for missing keys.

Parameters

keys (Iterable[str]) –

clear() None.  Remove all items from D.
get(k[, d]) D[k] if k in D, else d.  d defaults to None.
items() a set-like object providing a view on D’s items
keys() a set-like object providing a view on D’s keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D’s values