A transparent, persistent cache for the requests library
Project description
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.
See full project documentation at: https://requests-cache.readthedocs.io
Features
- Ease of use: Use as a drop-in replacement
for
requests.Session
, or install globally to add caching to allrequests
functions - Customization: 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, GridFS, DynamoDB, and filesystem.
- 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file requests-cache-0.7.2.tar.gz
.
File metadata
- Download URL: requests-cache-0.7.2.tar.gz
- Upload date:
- Size: 929.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0a1 CPython/3.9.6 Linux/5.4.0-1051-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6ae192defa2a5f45caf48f5f4f980fd6fe6f5f348470079224d8a5cd3858fee |
|
MD5 | 74639e0d5c48855c1e8f186b8c6f1b43 |
|
BLAKE2b-256 | 4e947c30526b19af0480f22d095e6bfe85aca3340c49fef5f28963ba1f2e8429 |
File details
Details for the file requests_cache-0.7.2-py3-none-any.whl
.
File metadata
- Download URL: requests_cache-0.7.2-py3-none-any.whl
- Upload date:
- Size: 49.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0a1 CPython/3.9.6 Linux/5.4.0-1051-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0be6dc60158a4ffa89935c9d652a121ddf758f467c43b34b752a2b8df2217a9d |
|
MD5 | 1b194f8b295e7a84d03af7141f4d0cad |
|
BLAKE2b-256 | 996dfb11c7ec8de0c81a7bb269729efb9a21a3aa6d0b18051174781fc95ee6c9 |