博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 缓存请求_在Python请求中缓存
阅读量:2518 次
发布时间:2019-05-11

本文共 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.

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

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

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

$ 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)

翻译自:

python 缓存请求

转载地址:http://vjqwd.baihongyu.com/

你可能感兴趣的文章
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>
一款jQuery打造的具有多功能切换的幻灯片特效
查看>>
SNMP从入门到开发:进阶篇
查看>>
@ServletComponentScan ,@ComponentScan,@Configuration 解析
查看>>
unity3d 射弹基础案例代码分析
查看>>
thinksns 分页数据
查看>>
os模块
查看>>
LINQ to SQL vs. NHibernate
查看>>
基于Angular5和WebAPI的增删改查(一)
查看>>
windows 10 & Office 2016 安装
查看>>
最短路径(SP)问题相关算法与模板
查看>>
js算法之最常用的排序
查看>>
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>
英特尔公司将停止910GL、915GL和915PL芯片组的生产
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>