This part of the documentation covers all the interfaces of requests-cache
Core functions for configuring cache and monkey patching requests
Requests Sessions with caching support.
Parameters: |
|
---|
Context manager for temporary disabling cache
>>> s = CachedSession()
>>> with s.cache_disabled():
... s.get('http://httpbin.org/ip')
Removes expired responses from storage
Installs cache for all Requests requests by monkey-patching Session
Parameters are the same as in CachedSession. Additional parameters:
Parameters: | session_factory – Session factory. It must be class which inherits CachedSession (default) |
---|
Installs cache for all Requests requests by monkey-patching Session
Parameters are the same as in CachedSession. Additional parameters:
Parameters: | session_factory – Session factory. It must be class which inherits CachedSession (default) |
---|
Restores requests.Session and disables cache
Context manager for temporary disabling globally installed cache
Warning
not thread-safe
>>> with requests_cache.disabled():
... requests.get('http://httpbin.org/ip')
... requests.get('http://httpbin.org/get')
Context manager for temporary installing global cache.
Accepts same arguments as install_cache()
Warning
not thread-safe
>>> with requests_cache.enabled('cache_db'):
... requests.get('http://httpbin.org/get')
Returns internal cache object from globally installed CachedSession
Clears globally installed cache
Removes expired responses from storage
Contains BaseCache class which can be used as in-memory cache backend or extended to support persistence.
Base class for cache implementations, can be used as in-memory cache.
To extend it you can provide dictionary-like objects for keys_map and responses or override public methods.
key -> key_in_responses mapping
key_in_cache -> response mapping
Save response to cache
Parameters: |
|
---|
Note
Response is reduced before saving (with reduce_response()) to make it picklable
Adds mapping of new_key to key_to_response to make it possible to associate many keys with single response
Parameters: |
|
---|---|
Returns: |
Retrieves response and timestamp for key if it’s stored in cache, otherwise returns default
Parameters: |
|
---|---|
Returns: | tuple (response, datetime) |
Note
Response is restored after unpickling with restore_response()
Delete key from cache. Also deletes all responses from response history
Delete response associated with url from cache. Also deletes all responses from response history. Works only for GET requests
Clear cache
Deletes entries from cache with creation time older than created_before
Returns True if cache has key, False otherwise
Returns True if cache has url, False otherwise. Works only for GET request urls
Reduce response object to make it compatible with pickle
Restore response object after unpickling
sqlite3 cache backend
sqlite cache backend.
Reading is fast, saving is a bit slower. It can store big amount of data with low memory usage.
Parameters: |
|
---|
mongo cache backend
mongo cache backend.
Parameters: |
|
---|
Dictionary-like objects for saving large data sets to sqlite database
DbDict - a dictionary-like object for saving large datasets to sqlite database
It’s possible to create multiply DbDict instances, which will be stored as separate tables in one database:
d1 = DbDict('test', 'table1')
d2 = DbDict('test', 'table2')
d3 = DbDict('test', 'table3')
all data will be stored in test.sqlite database into correspondent tables: table1, table2 and table3
Parameters: |
|
---|
Transactions can be commited if this property is set to True
Commits pending transaction if can_commit or force is True
Parameters: | force – force commit, ignore can_commit |
---|
Context manager used to speedup insertion of big number of records
>>> d1 = DbDict('test')
>>> with d1.bulk_commit():
... for i in range(1000):
... d1[i] = i * 2
Same as DbDict, but pickles values before saving
Parameters: |
|
---|
Dictionary-like objects for saving large data sets to mongodb database
MongoDict - a dictionary-like interface for mongo database
Parameters: |
|
---|
Same as MongoDict, but pickles values before saving
Parameters: |
|
---|
Dictionary-like objects for saving large data sets to redis key-store
RedisDict - a dictionary-like interface for redis key-stores
The actual key name on the redis server will be namespace:collection_name
In order to deal with how redis stores data/keys, everything, i.e. keys and data, must be pickled.
Parameters: |
|
---|