Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear cache with Python Requests?

Does the requests package of Python cache data by default?

For example,

import requests resp = requests.get('https://some website') 

Will the response be cached? If so, how do I clear it?

like image 234
ethanjyx Avatar asked Nov 25 '13 16:11

ethanjyx


People also ask

How do I clear data cache in python?

After the use of the cache, cache_clear() can be used for clearing or invalidating the cache. These methods have limitations as they are individualized, and the cache_clear() function must be typed out for each and every LRU Cache utilizing the function.

Does Python cache request?

Late answer, but python requests doesn't cache requests, you should use the Cache-Control and Pragma headers instead, i.e.: import requests h = { ... "Cache-Control": "no-cache", "Pragma": "no-cache" } r = requests.

Does Python request cache responses?

Requests-cache Now whenever you use requests , the response will be cached automatically.

Can you cache options requests?

OPTIONS requests aren't cacheable by default, so your CDN won't usually handle them, and this will have to hit your server every time. They are cached in clients, but only for 5 seconds by default. If a web page polls your API, making a request every 10 seconds, it'll repeat the preflight check every 10 seconds too.


1 Answers

Add a 'Cache-Control: no-cache' header:

self.request = requests.get('http://google.com',                             headers={'Cache-Control': 'no-cache'}) 

See https://stackoverflow.com/a/55613686/469045 for complete answer.

like image 83
jones77 Avatar answered Sep 23 '22 05:09

jones77