简体   繁体   English

在 Python 请求缓存 package 中,如何检测缓存命中或未命中?

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

The Python https://requests-cache.readthedocs.io/ library can be used to cache requests. Python https://requests-cache.readthedocs.io/库可用于缓存请求。 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?如果我正在使用请求缓存,我如何检测响应是来自缓存,还是必须从 .network 重新获取?

Based on the docs基于文档

The following attributes are available on responses:以下属性可用于响应:

  • from_cache : indicates if the response came from the cache from_cache :指示响应是否来自缓存
  • cache_key : The unique identifier used to match the request to the response (see Request Matching for details) cache_key :用于匹配请求到响应的唯一标识(详见请求匹配)
  • created_at : datetime of when the cached response was created or last updated created_at :创建或上次更新缓存响应的日期时间
  • expires : datetime after which the cached response will expire (see Expiration for details) expires :缓存的响应将过期的日期时间(有关详细信息,请参阅过期)
  • is_expired : indicates if the cached response is expired (if, for example, an old response was returned due to a request error) is_expired :指示缓存响应是否已过期(例如,如果由于请求错误而返回旧响应)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM