Requests-Cache¶
Summary¶
requests-cache is a transparent, persistent HTTP cache for the python requests library. It’s a convenient tool to use with web scraping, consuming REST APIs, slow or rate-limited sites, or any other scenario in which you’re making lots of requests that are expensive and/or likely to be sent more than once.
Features¶
Ease of use: Use as a drop-in replacement for
requests.Session
, or install globally to add caching to allrequests
functionsCustomization: Works out of the box with zero config, but with plenty of options available for customizing cache expiration and other behavior
Persistence: Includes several storage backends: SQLite, Redis, MongoDB, and DynamoDB.
Compatibility: Can be used alongside other popular libraries based on requests
Quickstart¶
First, install with pip:
pip install requests-cache
Next, use requests_cache.CachedSession to send and cache requests. To quickly demonstrate how to use it:
This takes ~1 minute:
import requests
session = requests.Session()
for i in range(60):
session.get('http://httpbin.org/delay/1')
This takes ~1 second:
import requests_cache
session = requests_cache.CachedSession('demo_cache')
for i in range(60):
session.get('http://httpbin.org/delay/1')
The URL in this example adds a delay of 1 second, simulating a slow or rate-limited website.
With caching, the response will be fetched once, saved to demo_cache.sqlite
, and subsequent
requests will return the cached response near-instantly.
If you don’t want to manage a session object, requests-cache can also be installed globally:
requests_cache.install_cache('demo_cache')
requests.get('http://httpbin.org/delay/1')
Next Steps¶
To find out more about what you can do with requests-cache, see:
The User Guide and Advanced Usage sections
A working example at Real Python: Caching External API Requests
More examples in the examples/ folder
Contents¶
- User Guide
- Advanced Usage
- Security
- Examples
- API Reference
- Contributing Guide
- Contributors
- Related Projects
- History
- 0.6.4 (2021-06-04)
- 0.6.3 (2021-04-21)
- 0.6.2 (2021-04-14)
- 0.6.1 (2021-04-13)
- Serialization
- Cache Expiration
- Backends
- Bugfixes
- General
- Docs & Tests
- 0.5.2 (2019-08-14)
- 0.5.1 (2019-08-13)
- 0.4.13 (2016-12-23)
- 0.4.12 (2016-03-19)
- 0.4.11 (2016-03-07)
- 0.4.10 (2015-04-28)
- 0.4.9 (2015-01-17)
- 0.4.8 (2014-12-13)
- 0.4.7 (2014-12-06)
- 0.4.6 (2014-10-13)
- 0.4.5 (2014-08-22)
- 0.4.4 (2013-10-31)
- 0.4.3 (2013-09-12)
- 0.4.2 (2013-08-25)
- 0.4.1 (2013-08-19)
- 0.2.1 (2013-01-13)
- 0.1.3 (2012-05-04)
- 0.1.2 (2012-05-02)
- 0.1.1 (2012-04-11)