From 7632de7c457bebf26b7503c8f7be3693b179b077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=B6ffner?= Date: Sat, 6 Mar 2021 01:29:06 +0100 Subject: Add individual expire_after per request. (#177) * Add individual expire_after per request. This commit adds a new feature to allow individual expire_after times per request. It involves using a custom CachedSession, PerRequestCachedSession, to be used as the session_factory. * Replace deprecated asserts in unit tests Additionally, some sockets are closed even on test failures. This removes some visual clutter when going through the results. * Update docs and README for per-request caching * Remove per_request and integrate behavior into CachedSession * Make test_return_old_data_on_error more reliable This is achieved by using a fixed date and mocking the backend calls to datetime as well. * Adjust per_request cache to new behavior Also adds HTTPBIN_URL environment variable as in test_cache * Add tests for the function determining the expiration datetime * Adjust backend to store expiration dates This also involves slightly different semantics for deletion, but the behavior stays the same. * Minor: black and isort * Update docs for request Add explanation for expire_after special cases, i.e. 'default' and 'cached' and how precedences are handled * Replace assert statements with methods * Remove invalid snippet from README * Remove invalid sentence from docs. * Make expires_before timezone-aware once And combine the checks for expiration * Simplify complexity by removing 'cached' keyword for expire_after --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'README.md') diff --git a/README.md b/README.md index 2a10d2e..417eeff 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,25 @@ It can be useful when you are creating some simple data scraper with constantly changing parsing logic or data format, and don't want to redownload pages or write complex error handling and persistence. +For more complex workflows, it is possible to cache different requests with different expiration times, or disable caching for a specific request completely: + + +For more complex workflows, it is possible to cache different requests with different expiry times, or disable caching for a specific request completely: + +```python +import time +import requests +import requests_cache + +requests_cache.install_cache('demo_cache') + +# Hits the URL only 2 times +for i in range(10): + requests.get('http://httpbin.org/delay/1', expire_after=1) + time.sleep(0.2) +``` + + ## Related Projects If `requests-cache` isn't quite what you need, you can help make it better! See the [Contributing Guide](https://github.com/reclosedev/requests-cache/blob/master/CONTRIBUTING.md) -- cgit v1.2.1