雅虎天气 API 上的服务器端缓存?
每次我发送请求时,我都会得到相同的天气,这似乎是雅虎天气 API 的一个错误。有趣的设计是,响应中没有任何时间戳字段来识别天气时间。
$ curl "http://weather.yahooapis.com/forecastjson?p=CHXX0099&u=c" -v
* About to connect() to weather.yahooapis.com port 80 (#0)
* Trying 180.233.115.34... connected
* Connected to weather.yahooapis.com (180.233.115.34) port 80 (#0)
> GET /forecastjson?p=CHXX0099&u=c HTTP/1.1
> User-Agent: curl/7.21.3 (i386-redhat-linux-gnu) libcurl/7.21.3 NSS/3.12.9.0 zlib/1.2.5 libidn/1.19 libssh2/1.2.7
> Host: weather.yahooapis.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 31 May 2011 12:58:10 GMT
< Cache-Control: private, max-age=1200
< Expires: Tue, 31 May 2011 13:18:10 GMT
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json;charset=UTF-8
<
* Closing connection #0
{"units":{"temperature":"C","speed":"km\/h","distance":"km","pressure":"mb"},"location":{"location_id":"CHXX0099","city":"Nanjing","state_abbreviation":"JI","country_abbreviation":"CH","elevation":3,"latitude":32,"longitude":118.80000000000000},"wind":{"speed":6.00000000000000,"direction":"N"},"atmosphere":{"humidity":"22","visibility":12.01000000000000,"pressure":1024.00000000000000,"rising":""},"url":"http:\/\/weather.yahoo.com\/forecast\/CHXX0099.html","logo":"http:\/\/l.yimg.com\/a\/i\/us\/nt\/ma\/ma_nws-we_1.gif","astronomy":{"sunrise":"06:22","sunset":"18:09"},"condition":{"text":"Sunny","code":"32","image":"http:\/\/l.yimg.com\/a\/i\/us\/we\/52\/32.gif","temperature":14.00000000000000},"forecast":[{"day":"Today","condition":"Mostly Clear","high_temperature":13.00000000000000,"low_temperature":3.00000000000000},{"day":"Tomorrow","condition":"Partly Cloudy","high_temperature":16.00000000000000,"low_temperature":5.00000000000000}]}
但是,如果我通过VPN请求相同的接口,返回的数据是正确的。
$ curl "http://weather.yahooapis.com/forecastjson?p=CHXX0099&u=c" -v
* About to connect() to weather.yahooapis.com port 80 (#0)
* Trying 180.233.115.34... connected
* Connected to weather.yahooapis.com (180.233.115.34) port 80 (#0)
> GET /forecastjson?p=CHXX0099&u=c HTTP/1.1
> User-Agent: curl/7.21.3 (i386-redhat-linux-gnu) libcurl/7.21.3 NSS/3.12.9.0 zlib/1.2.5 libidn/1.19 libssh2/1.2.7
> Host: weather.yahooapis.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 31 May 2011 12:58:10 GMT
< Cache-Control: private, max-age=1200
< Expires: Tue, 31 May 2011 13:18:10 GMT
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json;charset=UTF-8
<
* Closing connection #0
{"units":{"temperature":"C","speed":"km\/h","distance":"km","pressure":"mb"},"location":{"location_id":"CHXX0099","city":"Nanjing","state_abbreviation":"JI","country_abbreviation":"CH","elevation":3,"latitude":32,"longitude":118.80000000000000},"wind":{"speed":6.00000000000000,"direction":"N"},"atmosphere":{"humidity":"22","visibility":12.01000000000000,"pressure":1024.00000000000000,"rising":""},"url":"http:\/\/weather.yahoo.com\/forecast\/CHXX0099.html","logo":"http:\/\/l.yimg.com\/a\/i\/us\/nt\/ma\/ma_nws-we_1.gif","astronomy":{"sunrise":"06:22","sunset":"18:09"},"condition":{"text":"Sunny","code":"32","image":"http:\/\/l.yimg.com\/a\/i\/us\/we\/52\/32.gif","temperature":14.00000000000000},"forecast":[{"day":"Today","condition":"Mostly Clear","high_temperature":13.00000000000000,"low_temperature":3.00000000000000},{"day":"Tomorrow","condition":"Partly Cloudy","high_temperature":16.00000000000000,"low_temperature":5.00000000000000}]}
就我个人而言,我认为这与 Varnish 或 Squid 的东西有关。您是否也受到这个问题的影响?在找到官方支持链接之前,我该怎么做才能使服务器端缓存过期?
It seems a bug of Yahoo Weather API that every time I send a request I get the same weather. What's interesting design, there isn't any timestamp field in the response to identify the time of the weather.
$ curl "http://weather.yahooapis.com/forecastjson?p=CHXX0099&u=c" -v
* About to connect() to weather.yahooapis.com port 80 (#0)
* Trying 180.233.115.34... connected
* Connected to weather.yahooapis.com (180.233.115.34) port 80 (#0)
> GET /forecastjson?p=CHXX0099&u=c HTTP/1.1
> User-Agent: curl/7.21.3 (i386-redhat-linux-gnu) libcurl/7.21.3 NSS/3.12.9.0 zlib/1.2.5 libidn/1.19 libssh2/1.2.7
> Host: weather.yahooapis.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 31 May 2011 12:58:10 GMT
< Cache-Control: private, max-age=1200
< Expires: Tue, 31 May 2011 13:18:10 GMT
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json;charset=UTF-8
<
* Closing connection #0
{"units":{"temperature":"C","speed":"km\/h","distance":"km","pressure":"mb"},"location":{"location_id":"CHXX0099","city":"Nanjing","state_abbreviation":"JI","country_abbreviation":"CH","elevation":3,"latitude":32,"longitude":118.80000000000000},"wind":{"speed":6.00000000000000,"direction":"N"},"atmosphere":{"humidity":"22","visibility":12.01000000000000,"pressure":1024.00000000000000,"rising":""},"url":"http:\/\/weather.yahoo.com\/forecast\/CHXX0099.html","logo":"http:\/\/l.yimg.com\/a\/i\/us\/nt\/ma\/ma_nws-we_1.gif","astronomy":{"sunrise":"06:22","sunset":"18:09"},"condition":{"text":"Sunny","code":"32","image":"http:\/\/l.yimg.com\/a\/i\/us\/we\/52\/32.gif","temperature":14.00000000000000},"forecast":[{"day":"Today","condition":"Mostly Clear","high_temperature":13.00000000000000,"low_temperature":3.00000000000000},{"day":"Tomorrow","condition":"Partly Cloudy","high_temperature":16.00000000000000,"low_temperature":5.00000000000000}]}
However, if I request the same interface via VPN, the data returned is just right.
$ curl "http://weather.yahooapis.com/forecastjson?p=CHXX0099&u=c" -v
* About to connect() to weather.yahooapis.com port 80 (#0)
* Trying 180.233.115.34... connected
* Connected to weather.yahooapis.com (180.233.115.34) port 80 (#0)
> GET /forecastjson?p=CHXX0099&u=c HTTP/1.1
> User-Agent: curl/7.21.3 (i386-redhat-linux-gnu) libcurl/7.21.3 NSS/3.12.9.0 zlib/1.2.5 libidn/1.19 libssh2/1.2.7
> Host: weather.yahooapis.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 31 May 2011 12:58:10 GMT
< Cache-Control: private, max-age=1200
< Expires: Tue, 31 May 2011 13:18:10 GMT
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: application/json;charset=UTF-8
<
* Closing connection #0
{"units":{"temperature":"C","speed":"km\/h","distance":"km","pressure":"mb"},"location":{"location_id":"CHXX0099","city":"Nanjing","state_abbreviation":"JI","country_abbreviation":"CH","elevation":3,"latitude":32,"longitude":118.80000000000000},"wind":{"speed":6.00000000000000,"direction":"N"},"atmosphere":{"humidity":"22","visibility":12.01000000000000,"pressure":1024.00000000000000,"rising":""},"url":"http:\/\/weather.yahoo.com\/forecast\/CHXX0099.html","logo":"http:\/\/l.yimg.com\/a\/i\/us\/nt\/ma\/ma_nws-we_1.gif","astronomy":{"sunrise":"06:22","sunset":"18:09"},"condition":{"text":"Sunny","code":"32","image":"http:\/\/l.yimg.com\/a\/i\/us\/we\/52\/32.gif","temperature":14.00000000000000},"forecast":[{"day":"Today","condition":"Mostly Clear","high_temperature":13.00000000000000,"low_temperature":3.00000000000000},{"day":"Tomorrow","condition":"Partly Cloudy","high_temperature":16.00000000000000,"low_temperature":5.00000000000000}]}
Personally, I think this is something related by Varnish or Squid stuff. Are you also affected by this issue? Before I can find the official support link, what can I do to expire the server-side cache ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您发布的问题中,两个 Curl 示例看起来相同,但我想您注意到返回的数据根据您的网络路径而有所不同。
这些 JSON 响应有一定程度的缓存(我认为是 30 分钟),但您的网络上可能还有其他缓存。
一个重要的问题是后端的天气预报数据会定期更新,因为天气预报本身不会频繁变化。
事实上,您使用的 JSON 天气接口并未得到官方支持,但我相信它只是雅虎本身在各个页面上使用的。
请参阅 Yahoo! YDN 上的天气页面,提供“官方”支持的界面。该接口本身并没有那么有用,因为它只是 XML 格式。相反,您可以使用 YQL 控制台 传递您的位置并接收返回 JSON 格式的数据。
例如,尝试以下查询: 从weather.bylocation中选择*,其中location="中国南京"。在结果中,请注意
lastBuildData
值,它是上次更新预测的时间:In your question as posted, the two Curl examples look identical, but I imagine you are noticing that the data returned was different depending on your network path.
There is some level of caching on these JSON responses (30 minutes I believe), but there may be additional caching on your network.
One important issue is the weather forecast data on the backend is updated on intervals, because the forecast itself does not change that frequently.
In fact, the JSON weather interface you're using is not officially supported, but I believe it's just used by Yahoo itself on various pages.
Refer to the Yahoo! Weather page on YDN for the "official" supported interface. By itself this interface is not as useful because it's only in XML format. Instead you could use the YQL console to pass in your location and receive back JSON formatted data.
For example, try this query: select * from weather.bylocation where location="Nanjing, CN". In the results, notice the
lastBuildData
value which is the last time the forecast was updated:我不完全确定它是否同样适用于此,但针对 IE 对 GET 请求进行积极缓存的常见解决方案是在请求中添加时间戳参数 - 由于每次发出请求时该值都会发生变化,因此每个请求看起来都是唯一的。
选择一个不太可能与 API 参数冲突的参数名称,并在每次发出请求时将其设置为当前时间,看看是否可以解决问题。
I'm not entirely sure if it's equally applicable here but a common solution to IE's aggressive caching for GET requests is to add a timestamp parameter in the request - since the value changes each time the request is made, each request appears unique.
Choose a parameter name that is unlikely to conflict with the API parameters and set it to the current time each time you make the request and see if that fixes things.