Reputation: 41817
The Python https://requests-cache.readthedocs.io/ library can be used to cache requests. If I'm using requests-cache, how do I detect whether a response came from the cache, or had to be re-fetched from the network?
Upvotes: 7
Views: 2214
Reputation: 118011
Based on the docs
The following attributes are available on responses:
from_cache
: indicates if the response came from the cachecache_key
: The unique identifier used to match the request to the response (see Request Matching for details)created_at
: datetime of when the cached response was created or last updatedexpires
: datetime after which the cached response will expire (see Expiration for details)is_expired
: indicates if the cached response is expired (if, for example, an old response was returned due to a request error)
From their example
from requests_cache import CachedSession
session = CachedSession(expire_after=timedelta(days=1))
response = session.get('http://httpbin.org/get')
print(response.from_cache)
Upvotes: 10