在open-uri中禁用缓存

问题描述

可悲的是,当数据更改时,我必须轮询一个端点并更新另一个系统。我编写了一个循环(使用sleep语句,所以我不使用DOS服务器):

require 'nokogiri'
require 'open-uri'

desired_data = 'foo'
data = nil
url = nil

while data != desired_data do
  sleep(2)
  url = "https://elections.wi.gov/index.PHP/elections-voting/statistics"
  doc = Nokogiri::HTML.parse(open(url))

  puts doc

  # do some nokogiri stuff to extract the @R_546_4045@ion I want.

  # store @R_546_4045@ion to `data` variable.
end

# if control is here it means the data changed

除了服务器更新时,open(url)仍返回旧内容(即使我重新启动脚本)也是如此。

似乎正在播放一些缓存。如何禁用它?

以下是返回的HTTP标头:

HTTP/2 200
date: Fri,02 Oct 2020 14:00:44 GMT
content-type: text/html; charset=UTF-8
set-cookie: __cfduid=dd8fca84d468814dd199dfc08d45c98831601647244; expires=Sun,01-Nov-20 14:00:44 GMT; path=/; domain=.elections.wi.gov; HttpOnly; SameSite=Lax; Secure
x-powered-by: PHP/7.2.24
cache-control: max-age=3600,public
x-drupal-dynamic-cache: MISS
link: <https://elections.wi.gov/index.PHP/elections-voting/statistics>; rel="canonical"
x-ua-compatible: IE=edge
content-language: en
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
expires: Sun,19 Nov 1978 05:00:00 GMT
last-modified: Fri,02 Oct 2020 12:47:38 GMT
vary: Cookie
x-generator: Drupal 8 (https://www.drupal.org)
x-drupal-cache: HIT
x-speed-cache: HIT
x-speed-cache-key: /index.PHP/elections-voting/statistics
x-nocache: Cache
x-this-proto: https
x-server-name: elections.wi.gov
access-control-allow-origin: *
x-xss-protection: 1; mode=block
cache-control: no-store,no-cache,must-revalidate,post-check=0,pre-check=0
cf-cache-status: DYNAMIC
cf-request-id: 058b368b9f00002ff234177200000001
expect-ct: max-age=604800,report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
cf-ray: 5dbef38c3b6a2ff2-ORD```

If it matters,I’m using Ruby 2.7 on macOS Big Sur. 

解决方法

Drupal 8网站本身可能有一个问题,因为它具有自己的缓存管理器-如果您使用网络浏览器获取新内容,似乎每个用户都有一个缓存。

很容易看到某个页面改变了哪些缓存上下文,以及哪个页面使哪个缓存标签无效:仅需查看X-Drupal-Cache-Contexts和X-Drupal-Cache-Tags标头即可!

但是这些标题在您的列表中不可用。如果您与网站的开发人员联系,请他们执行以下操作:

您可以通过在services.yml中将http.response.debug_cacheability_headers容器参数设置为true来调试可缓存的响应(实现此接口的响应,该响应可能由页面缓存或动态页面缓存进行缓存)。继之以容器重建,这在更改容器参数时是必需的。

这将导致Drupal发送X-Drupal-Cache-Tag和X-Drupal-Cache-Contexts标头。