【问题标题】:Server side cache on Yahoo Weather API?Yahoo Weather API 上的服务器端缓存?
【发布时间】:2011-09-05 11:51:13
【问题描述】:

雅虎天气 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 的东西有关。你是否也受到这个问题的影响?在我找到官方支持链接之前,我该怎么做才能使服务器端缓存过期?

【问题讨论】:

    标签: yahoo weather yahoo-api


    【解决方案1】:

    我不完全确定它是否在这里同样适用,但 IE 对 GET 请求的积极缓存的一个常见解决方案是在请求中添加时间戳参数 - 因为每次发出请求时值都会改变,每个请求看起来都是唯一的.

    选择一个不太可能与 API 参数冲突的参数名称,并将其设置为每次发出请求时的当前时间,看看是否能解决问题。

    【讨论】:

    • 谢谢。我使用随机参数进行了测试,但它不适用于雅虎的固态缓存:(
    【解决方案2】:

    在您发布的问题中,两个 Curl 示例看起来相同,但我想您注意到返回的数据因您的网络路径而异。

    这些 JSON 响应有一定程度的缓存(我相信是 30 分钟),但您的网络上可能还有额外的缓存。

    一个重要的问题是后端的天气预报数据会定期更新,因为预报本身并不会那么频繁地变化。

    其实你使用的JSON天气接口并没有官方支持,但我相信它只是雅虎自己在各个页面上使用的。

    “官方”支持的接口请参考Yahoo! Weather page on YDN。这个接口本身并没有那么有用,因为它只是 XML 格式。相反,您可以使用 YQL console 传递您的位置并接收返回 JSON 格式的数据。

    例如,试试这个查询:select * from weather.bylocation where location="Nanjing, CN"。在结果中,请注意 lastBuildData 值,它是预测的最后一次更新:

    {
     "query": {
      "count": 1,
      "created": "2011-12-30T20:03:17Z",
      "lang": "en-US",
      "results": {
       "weather": {
        "rss": {
         "version": "2.0",
         "geo": "http://www.w3.org/2003/01/geo/wgs84_pos#",
         "yweather": "http://xml.weather.yahoo.com/ns/rss/1.0",
         "channel": {
          "title": "Yahoo! Weather - Nanjing, CN",
          "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Nanjing__CN/*http://weather.yahoo.com/forecast/CHXX0099_f.html",
          "description": "Yahoo! Weather for Nanjing, CN",
          "language": "en-us",
          "lastBuildDate": "Sat, 31 Dec 2011 3:00 am CST",
          "ttl": "60",
          "location": {
           "city": "Nanjing",
           "country": "China",
           "region": ""
          },
          "units": {
           "distance": "mi",
           "pressure": "in",
           "speed": "mph",
           "temperature": "F"
          },
          "wind": {
           "chill": "25",
           "direction": "90",
           "speed": "4"
          },
          "atmosphere": {
           "humidity": "83",
           "pressure": "30.45",
           "rising": "2",
           "visibility": "0.99"
          },
          "astronomy": {
           "sunrise": "7:03 am",
           "sunset": "5:09 pm"
          },
          "image": {
           "title": "Yahoo! Weather",
           "width": "142",
           "height": "18",
           "link": "http://weather.yahoo.com",
           "url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
          },
          "item": {
           "title": "Conditions for Nanjing, CN at 3:00 am CST",
           "lat": "32.05",
           "long": "118.78",
           "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Nanjing__CN/*http://weather.yahoo.com/forecast/CHXX0099_f.html",
           "pubDate": "Sat, 31 Dec 2011 3:00 am CST",
           "condition": {
            "code": "20",
            "date": "Sat, 31 Dec 2011 3:00 am CST",
            "temp": "30",
            "text": "Fog"
           },
           "description": "
    \nCurrent Conditions:
    \nFog, 30 F
    \n
    Forecast:
    \nSat - Sunny. High: 50 Low: 28
    \nSun - Sunny. High: 51 Low: 28
    \n
    \nFull Forecast at Yahoo! Weather

    \n(provided by The Weather Channel)
    ", "forecast": [ { "code": "32", "date": "31 Dec 2011", "day": "Sat", "high": "50", "low": "28", "text": "Sunny" }, { "code": "32", "date": "1 Jan 2012", "day": "Sun", "high": "51", "low": "28", "text": "Sunny" } ], "guid": { "isPermaLink": "false", "content": "CHXX0099_2012_01_01_7_00_CST" } } } } } } } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      相关资源
      最近更新 更多