本文共 3317 字,大约阅读时间需要 11 分钟。
python 缓存请求
I think I’ve made it clear in the past that I think is awesome. At this stage it’s become a mature, feature-filled library that is more than capable of replacing urllib2 and friends in almost every situation you might be interested in. There are very few things that urllib2 can do that Requests can’t do, and Requests is almost always capable of doing it better.
我认为我过去曾明确表示我认为非常棒。 在这个阶段,它已经成为一个成熟的,功能丰富的库,它几乎可以在您可能感兴趣的每种情况下替换urllib2和朋友。urllib2可以做的事情很少,而Requests不能做,而Requests几乎总是有能力做得更好。
However, one of things urllib2 can do that Requests can’t do (out of the box) is caching. This is a shame, since HTTP caching has effectively made the modern internet.
但是,urllib2可以执行的请求(开箱即用)不能做的一件事就是缓存。 很遗憾,因为HTTP缓存有效地使现代互联网成为现实。
Writing your own HTTP cache isn’t really very hard: and there isn’t actually that much functionality. You could make pretty major gains just by supporting the Cache-Control header, and realistically that doesn’t take much work at all.
编写自己的HTTP缓存并不是很困难: ,实际上并没有那么多功能。 仅通过支持Cache-Control标头,您就可以取得相当大的收获,而实际上这根本不需要太多工作。
That’s a hassle, though. Requests gives you so much: why should you have to do this yourself? What you really need is a Requests plugin that makes HTTP caching ‘just work’.
不过,这很麻烦。 请求给您带来了很多:为什么您必须自己做呢? 您真正需要的是一个使HTTP缓存“正常工作”的Requests插件。
You’ll never guess what I’ve been doing.
你永远不会猜到我在做什么。
You want caching? You’re using Requests? Here’s how you get caching with minimal work. Crack out your command prompt and install my brand new module, :
您要缓存吗? 您正在使用请求吗? 这是您以最少的工作即可获得缓存的方法。 破解命令提示符并安装我的全新模块 :
$ pip install httpcache$ pip install httpcache
Once you’ve installed it, you get caching like this:
安装后,您将获得如下所示的缓存:
Done.
做完了
Really, I mean done. Everything just works. All your HTTP traffic passes through my caching adapter which handles all the busy work. You can just sit there and reap the benefits of decreased bandwidth usage and shorter latency.
真的,我是说完成。 一切正常。 您所有的HTTP流量都通过我的缓存适配器进行处理,该适配器处理所有繁忙的工作。 您可以坐在那里,获得减少带宽使用和缩短延迟的好处。
Using httpcache gives you lots of things. Here are the highlights:
使用httpcache可为您提供许多帮助。 以下是重点内容:
All of this, and you don’t have to do a thing. The life of a Python programmer is pretty awesome sometimes.
所有这些,您不必做任何事情。 有时,Python程序员的生活非常出色。
Like Requests, httpcache has some very strong opinions about ‘the right thing’. For that reason, you don’t get much in the way of configuration options. In fact, you get exactly one: the cache capacity. You know, so you don’t run out of memory and fall over.
像请求一样,httpcache对于“正确的事情”也有一些非常强烈的意见。 出于这个原因,您在配置选项方面的了解并不多。 实际上,您得到的正是一个:缓存容量。 您知道的,这样您就不会耗尽内存并跌倒。
翻译自:
python 缓存请求
转载地址:http://vjqwd.baihongyu.com/