python 缓存请求_在Python请求中缓存

本文介绍了如何使用httpcache模块在Requests库中轻松实现HTTP缓存功能,减少带宽使用和提高响应速度。httpcache与Requests紧密集成,自动处理Cache-Control和Expires等头部信息,支持幂等性检查和验证缓存。
摘要由CSDN通过智能技术生成

python 缓存请求

I think I’ve made it clear in the past that I think Requests 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.

我认为我过去曾明确表示我认为Requests非常棒。 在这个阶段,它已经成为一个成熟的,功能丰富的库,它几乎可以在您可能感兴趣的每种情况下替换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: RFC 2616 is pretty clear about how it works 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缓存并不是很困难: RFC 2616非常清楚其工作原理 ,实际上并没有那么多功能。 仅通过支持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.

你永远不会猜到我在做什么。

httpcache:缓存请求 (httpcache: Caching For Requests)

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, httpcache:

您要缓存吗? 您正在使用请求吗? 这是您以最少的工作即可获得缓存的方法。 破解命令提示符并安装我的全新模块httpcache

$ 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流量都通过我的缓存适配器进行处理,该适配器处理所有繁忙的工作。 您可以坐在那里,获得减少带宽使用和缩短延迟的好处。

你得到了什么? (What Do You Get?)

Using httpcache gives you lots of things. Here are the highlights:

使用httpcache可为您提供许多帮助。 以下是重点内容:

  • Tight integration with Requests. Plugs in and just works.
  • Cache-Control headers are understood, in all their complicated glory.
  • Expires headers are understood, in all their HTTP/1.0 retroness.
  • Responses to non-idempotent messages aren’t cached.
  • Non-idempotent messages invalidate cached responses, as per RFC 2616.
  • Performs validation caching: If-Modified-Since headers and HTTP 304 responses.
  • 与请求紧密集成。 插入电源即可使用。
  • 可以理解Cache-Control标头的所有复杂方面。
  • Expires标头以其所有的HTTP / 1.0风格被理解。
  • 对非幂等消息的响应不会被缓存。
  • 根据RFC 2616,非幂等消息会使缓存的响应无效。
  • 执行验证缓存:If-Modified-Since标头和HTTP 304响应。

All of this, and you don’t have to do a thing. The life of a Python programmer is pretty awesome sometimes.

所有这些,您不必做任何事情。 有时,Python程序员的生活非常出色。

我知道什么最适合你 (I Know What’s Best For You)

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对于“正确的事情”也有一些非常强烈的意见。 出于这个原因,您在配置选项方面的了解并不多。 实际上,您得到的正是一个:缓存容量。 您知道的,这样您就不会耗尽内存并跌倒。

贡献和进一步的工作 (Contributions And Further Work)

翻译自: https://www.pybloggers.com/2013/05/caching-in-python-requests/

python 缓存请求

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值