在 Python requests-cache 包中,如何检测缓存命中或未命中?

In the Python requests-cache package, how do I detect a cache hit or miss?

Python https://requests-cache.readthedocs.io/ 库可用于缓存请求。如果我使用请求缓存,我如何检测响应是来自缓存,还是必须从网络重新获取?

基于the docs

The following attributes are available on responses:

  • from_cache: indicates if the response came from the cache
  • cache_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 updated
  • expires: 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 requests_cache import CachedSession
session = CachedSession(expire_after=timedelta(days=1))

response = session.get('http://httpbin.org/get')
print(response.from_cache)