-->

使用缓存的饲料,防止饲料谷歌API(Prevent Google Feed API from usi

2019-07-03 20:37发布

我使用的是谷歌订阅JSAPI读/解析饲料。 问题是,当饲料的变化,以前的条目变得无效(链接和图像不工作),所以我不能加载饲料的缓存版本。 我以为装载饲料时,不使用缓存的版本,但我没有看到一个会有一个选项。 我的解决办法是做在一个变量(T)添加到饲料URL的末尾,以便它是“唯一”但这似乎哈克(但它的工作原理)。 任何人都知道一个更好的方式来做到这一点?

    function onLoad() {
      // Create a feed instance that will grab feed feed.
      var feed = new google.feeds.Feed(feedLocation+"&t="+new Date().getTime());

      // Request the results in XML (so that we can parse out all the info
      feed.setResultFormat(google.feeds.Feed.XML_FORMAT);

      //must set this - if you don't, it defaults to 4
      feed.setNumEntries(50);

      // Calling load sends the request off.  It requires a callback function.
      feed.load(feedLoaded);
    }

Answer 1:

这个答案可能会晚来,但是我遇到了这个职位,并得到使用该解决方案pxpgraphics评论。 对于那些谁需要缓存失效,这将做到这一点。 请注意,此代码的例子是从RSS馈送拉动的其他数据; 修改您的需求。

google.load("feeds", "1");

function initialize() {
    var feedLocation = "http://feeds.bbci.co.uk/news/rss.xml";

    /*  Use the randomNum and the time to invalidate the Google cache and 
        fetch the latest articles.
    */
    var randomNum = Math.floor((Math.random() * 10000) + 1);
    var url = feedLocation + "?&t=" + new Date().getTime() + randomNum;

    var feed = new google.feeds.Feed(url);
    feed.setNumEntries(1000);
    feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        var div = document.createElement("div");
        div.appendChild(document.createTextNode(entry.title));
        div.innerHTML += "<br>" + entry.publishedDate;
        div.innerHTML += "<br>" + entry.content;
        container.appendChild(div);
      }
    }
  });
}
google.setOnLoadCallback(initialize);

此代码假定你有这个DIV在页面上:

<div id="feed"></div>


Answer 2:

尝试feed.includeHistoricalEntries(); 它可能会解决您的问题。



文章来源: Prevent Google Feed API from using cached feed