cached_requests
cached_requests is a Python library that provides a simple and effective caching layer for your web requests. It's built on top of the popular requests library and is designed to be a drop-in replacement for requests.Session.
Features
- Persistent Caching: Save responses to disk to speed up repeated requests.
- Automatic Cache Invalidation: Set a time-to-live (TTL) for your cached responses.
- Customizable: Configure cache directories, refresh policies, and more.
- Easy to Use: A simple, intuitive API that gets out of your way.
Installation
Install CacheFlow using pip:
pip install cached_requests
Quick Start
Here's a simple example of how to use cached_requests:
from cached_requests import CacheSession
from datetime import timedelta
# Create a new session with a cache directory
requests = CacheSession(cache_dir='.cache', refresh_after=timedelta(hours=1)) # or use `from cached_requests import requests`
# Make a request
response = requests.get('https://api.github.com')
# The response is now cached. Subsequent requests to the same URL will be served from the cache.
cached_response = requests.get('https://api.github.com')
print(response.json())
Advanced Usage
Configuration
You can configure the behavior of cached_requests by passing arguments to the CacheSession constructor:
cache_dir: The directory where cached responses will be stored.force_refresh: IfTrue, the cache will be ignored and all requests will be made to the network.refresh_after: Atimedeltaobject that specifies how long a cached response is valid.refresh_on_error: IfTrue, the cache will be refreshed if a cached response resulted in an error.
Context Manager
You can also use a context manager to temporarily change the configuration:
with requests.configure(force_refresh=True):
# This request will bypass the cache
response = requests.get('https://api.github.com')
Deleting Cache Entries
CacheFlow provides multiple ways to manage your cache.
Deleting by URL
You can delete cache entries based on a URL pattern:
from cached_requests import delete_cache_by_function
def should_delete(url: str):
return 'github.com' in url
delete_cache_by_function(requests, should_delete)
Deleting by Expiration
You can delete cache entries that are older than a specified timedelta:
from cached_requests import delete_cache_by_expiration
from datetime import timedelta
# Delete all cache entries older than 7 days
delete_cache_by_expiration(requests, timedelta(days=7))
Contributing
Contributions are welcome! If you have a feature request, bug report, or pull request, please open an issue on GitHub.
License
cached_requests is licensed under the MIT License. See the LICENSE file for more details.
Metadata
Release files for cached-requests 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cached_requests-0.2.2.tar.gz | 10.4 kB | Without provenance | Details for cached_requests-0.2.2.tar.gz |
Release files / cached_requests-0.2.2.tar.gz
| Download URL | cached_requests-0.2.2.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e739dcff5fce04b64830be5699791de7c48575db60b0d202199bdf0913b84592
|
|
BLAKE2b-256 checksum How to use checksums |
5740c0c6748f1fe1ca3f3266628e760183515ebfaa4d04336a431f391d929de1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Security for cached-requests 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.