- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用的是 YouTube 数据 API v3。
是否有可能制作一个大的BatchHttpRequest
(例如,参见 here )并在 httplib2
级别使用 ETag 进行本地缓存(例如,参见 here )?
ETags 对于单个查询工作得很好,我不知道它们是否对批量请求也有用。
最佳答案
长话短说:
在这里:
先看看BatchHttpRequest
的初始化方式:
from apiclient.http import BatchHttpRequest
def list_animals(request_id, response, exception):
if exception is not None:
# Do something with the exception
pass
else:
# Do something with the response
pass
def list_farmers(request_id, response):
"""Do something with the farmers list response."""
pass
service = build('farm', 'v2')
batch = service.new_batch_http_request()
batch.add(service.animals().list(), callback=list_animals)
batch.add(service.farmers().list(), callback=list_farmers)
batch.execute(http=http)
其次让我们看看如何使用 ETag
:
from google.appengine.api import memcache
http = httplib2.Http(cache=memcache)
现在让我们分析一下:
观察 BatchHttpRequest 示例的最后一行:batch.execute(http=http)
,现在检查 source code对于执行,它调用 _refresh_and_apply_credentials
,它应用我们传递给它的 http 对象。
def _refresh_and_apply_credentials(self, request, http):
"""Refresh the credentials and apply to the request.
Args:
request: HttpRequest, the request.
http: httplib2.Http, the global http object for the batch.
"""
# For the credentials to refresh, but only once per refresh_token
# If there is no http per the request then refresh the http passed in
# via execute()
这意味着,接受 http 的执行调用可以传递给您创建的 ETag http:
http = httplib2.Http(cache=memcache)
# This would mean we would get the ETags cached http
batch.execute(http=http)
更新 1:
也可以尝试使用自定义对象:
from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE
from googleapiclient.discovery_cache.base import Cache
from googleapiclient.discovery_cache.file_cache import Cache as FileCache
custCache = FileCache(max_age=DISCOVERY_DOC_MAX_AGE)
http = httplib2.Http(cache=custCache)
# This would mean we would get the ETags cached http
batch.execute(http=http)
因为,这只是对 http2 库中注释的预感:
"""If 'cache' is a string then it is used as a directory name for
a disk cache. Otherwise it must be an object that supports the
same interface as FileCache.
再次验证 google-api-python 源代码后,我看到,BatchHttpRequest
已修复为 'POST'
请求,并且内容类型为 多部分/混合;..
- source code
.
提供有关以下事实的线索,BatchHttpRequest 可用于POST
数据,然后在后面进行处理。
现在,牢记这一点,观察什么 httplib2
request
方法使用:_updateCache
仅当满足以下条件时:
["GET", "HEAD"]
或 response.status == 303
或者是 redirect request
<response.status in [200, 203] and method in ["GET", "HEAD"]
if response.status == 304 and method == "GET"
这意味着,BatchHttpRequest
不能与缓存一起使用。
关于python - 谷歌 API 客户端(Python): is it possible to use BatchHttpRequest with ETag caching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37797374/
更新的问题 我的应用程序如何利用 etags,引入流/分块编码是否会带来任何复杂性? 原始问题 使用 Transfer-Encoding: chunked 进行 HTTP 流传输时, Content-
有谁知道在托管在单个网络服务器上的网络应用程序上禁用 ETag 是否值得?目前我们不在我们的应用程序中使用 ETag。 如果值得禁用它们 - 为什么? 非常感谢。 最佳答案 我不知道这是否有帮助,但您
我需要能够在以下条件下将实体插入到 azure 存储表中: 如果不存在,则插入。 如果存在,但我指定ETag为*,则替换。 如果存在,但 ETag 有另一个值,则抛出 StorageException
javascript 是否可以读取 HTTP 响应的 ETAG 值? 在向服务器的 HTTP 请求中写出 ETAG 值是否有任何问题? 最佳答案 如果它是一个定期加载的页面,我认为 HTTP 请求/响
标准的 GNU etags 不支持目录的递归遍历,就像 exuberant ctags -R 所做的那样。如果我只能访问 GNU etags,我如何使用 bash shell magic 让 etag
我无法可靠地在用户浏览器上设置 Etag。当用户点击我的一个外部链接时,我想将文章 ID 设置到他们的 Etag 中(我也使用 cookie,但我想专门试验 Etag 以测试其可靠性)。 当同一个用户
我已经通过 .htaccess 为静态站点设置了浏览器缓存通过设置文件: # BROWER CACHING - 1 Day for images Header set Cache-Control "
我了解使用 etags 进行乐观并发控制(例如,在 RESTful 架构风格中),并且我读过 etags 对于同一资源的不同表示应该是不同的。这是为什么? 最终我们是否有兴趣知道资源是否已更改以便我们
我对 OkHttp 以及它如何选择进行缓存有一些疑问: OkHttp 是否支持弱 ETag(在 Tomcat 7 中默认使用)?我在某处读到 Chrome 会忽略任何支持 Last-Modified
我开发了一个 ASP.NET Core 中间件解决方案,它计算与我正在执行的 GET 请求匹配的 ETag 的值,并在响应中返回它。 相关代码摘录如下: RequestHeaders requestH
我最近了解了 ETag HTTP header 的概念。 ( this ) 但我仍然有一个问题,对于负责生成 ETag 的特定 HTTP 资源? 换句话说,就是实际应用、容器(Ex:Tomcat)、W
我想为我的内容构建强大的 etag,您可以在图片中查看它。 在新选项卡中打开图像以查看大图.. 最佳答案 您对文件内容使用强大的哈希算法,例如 md5 或 sha-1。 HTTP 规范中有关于弱 et
最近发现了与 HTTP ETag 有关的问题和我们的 CDN我试图在 Fiddler 中为知名站点捕获一些。然而,无论我使用何种浏览器/网站组合,我都没有看到任何经过。 这有什么原因吗?你能建议我可以
许多 REST API 提供了搜索资源的能力。 例如,可以使用以下 HTTP 请求获取类型 A 的资源: GET /A?prop1={value1}&prop2={value2} 我正在使用乐观锁定,
即使我发送“cache-control: must-revalidate”,谷歌浏览器在浏览器中使用前后按钮时也会使用本地缓存页面。 这是原始回复的一部分: HTTP/1.1 200 OK cache
Redbot报告我的网页标题无效: The ETag header's syntax isn't valid. 我的标题设置为: ETag: 4ae413bd 为什么无效? ETag 的语法是什么?
HTTP 协议(protocol)是否规定了 ETag 的最大长度? 最佳答案 No . 14.19 ETag The ETag response-header field provides the
我一直在读一本书,我对 ETag 章节有一个特别的疑问。作者说 ETag 可能会损害性能,您必须对它们进行微调或完全禁用它们。 我已经知道什么是 ETag 并了解其中的风险,但是正确使用 ETag 有
我在做什么 我从数据库中提取图像并将其与所有正确的 header 一起发送到浏览器 - 图像显示正常。我还发送了一个 ETag header ,使用图像内容的 SHA1 作为标签。 图片半定期调用,所
我在更新存储桶 S3 时尝试使用 etag,但出现此错误: Error: Error in function call on config.tf line 48, in resource "aw
我是一名优秀的程序员,十分优秀!