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.

1 answer

  • answered 2017-12-06 01:41 alecxe

    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