Python https://requests-cache.readthedocs.io/库可用于缓存请求.如果我使用的是请求缓存,如何检测响应是来自缓存,还是必须从网络中重新获取?
Python https://requests-cache.readthedocs.io/库可用于缓存请求.如果我使用的是请求缓存,如何检测响应是来自缓存,还是必须从网络中重新获取?
以the docs为基础
以下属性可用于响应:
- 100: indicates if the response came from the cache
cache_key
:用于将请求与响应匹配的唯一标识符(有关详细信息,请参阅请求匹配)created_at
:创建或上次更新缓存响应的日期时间expires
:缓存响应过期的日期时间(有关详细信息,请参阅过期)is_expired
:指示缓存的响应是否过期(例如,如果由于请求错误返回了旧响应)
从他们的例子
from requests_cache import CachedSession
session = CachedSession(expire_after=timedelta(days=1))
response = session.get('http://httpbin.org/get')
print(response.from_cache)