Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way I can log when python "requests-cache" hits the cache?

I'm using https://requests-cache.readthedocs.io/en/latest/index.html and I want to know when the cache is hit vs. the web.

like image 337
avv Avatar asked Dec 06 '17 01:12

avv


1 Answers

According to the source code, there should be a .from_cache flag set on the response object:

In [1]: import requests

In [2]: import requests_cache

In [3]: requests_cache.install_cache('demo_cache')

In [4]: requests.get("https://stackoverflow.com").from_cache
Out[4]: False

In [5]: requests.get("https://stackoverflow.com").from_cache
Out[5]: True
like image 171
alecxe Avatar answered Nov 10 '22 01:11

alecxe