MongoDB#
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
:
>>> backend = MongoCache(host='192.168.1.63', port=27017)
>>> session = CachedSession('http_cache', backend=backend)
API Reference#
MongoDB cache backend |
|
A dictionary-like interface for a MongoDB collection |
|
Same as |
- class requests_cache.backends.mongodb.MongoCache(db_name='http_cache', connection=None, **kwargs)[source]#
Bases:
requests_cache.backends.base.BaseCache
MongoDB cache backend
- Parameters
db_name (
str
) – Database nameconnection (
Optional
[MongoClient
]) –pymongo.MongoClient
object to reuse instead of creating a new onekwargs – Additional keyword arguments for
pymongo.mongo_client.MongoClient
- bulk_delete(keys)#
Remove multiple responses and their associated redirects from the cache
- clear()#
Delete all items from the cache
- contains(key=None, request=None, url=None)#
Check if the specified request is cached
- Parameters
request (
Union
[Request
,PreparedRequest
,CachedRequest
,None
]) – Check for a matching request, according to current request matching settingsurl (
Optional
[str
]) – Check for a matching GET request with the specified URL
- create_key(request=None, **kwargs)#
Create a normalized cache key from a request object
- Parameters
request (
Union
[Request
,PreparedRequest
,CachedRequest
,None
]) –- Return type
- delete(*keys, expired=False, invalid=False, requests=None, urls=None)#
Remove responses from the cache according one or more conditions.
- Parameters
keys (
str
) – Remove responses with these cache keysexpired (
bool
) – Remove all expired responsesinvalid (
bool
) – Remove all invalid responses (that can’t be deserialized with current settings)requests (
Optional
[Iterable
[Union
[Request
,PreparedRequest
,CachedRequest
]]]) – Remove matching responses, according to current request matching settingsurls (
Optional
[Iterable
[str
]]) – Remove matching GET requests for the specified URL(s)
- filter(valid=True, expired=True, invalid=False)#
Get responses from the cache, with optional filters
- Parameters
- Return type
- get_response(key, default=None)#
Retrieve a response from the cache, if it exists
- Parameters
key (
str
) – Cache key for the responsedefault – Value to return if key is not in the cache
- Return type
- remove_expired_responses(expire_after=None)#
- reset_expiration(expire_after=None)#
Set a new expiration value on existing cache items
- save_response(response, cache_key=None, expires=None)#
Save a response to the cache
- 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
connection –
pymongo.MongoClient
object to reuse instead of creating a new onekwargs – Additional keyword arguments for
pymongo.MongoClient
- bulk_delete(keys)[source]#
Delete multiple keys from the cache. Does not raise errors for missing keys.
- 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.
- property serializer#
- 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.
- 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.
- property serializer#
- 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 #