阻止 Google Feed API 使用缓存的 Feed [英] Prevent Google Feed API from using cached feed

查看:27
本文介绍了阻止 Google Feed API 使用缓存的 Feed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google Feed JSAPI 来读取/解析 Feed.问题是,当提要更改时,以前的条目将无效(链接和图像不起作用),因此我无法加载提要的缓存版本.我认为加载提要时会有一个选项不使用缓存版本,但我没有看到.我的解决方案是在提要 url 的末尾添加一个变量 (t),因此它是唯一的",但这似乎很糟糕(但它有效).有人知道更好的方法吗?

 函数 onLoad() {//创建一个将获取提要提要的提要实例.var feed = new google.feeds.Feed(feedLocation+"&t="+new Date().getTime());//请求 XML 格式的结果(这样我们就可以解析出所有的信息feed.setResultFormat(google.feeds.Feed.XML_FORMAT);//必须设置 - 如果不设置,则默认为 4feed.setNumEntries(50);//调用 load 发送请求.它需要一个回调函数.feed.load(feedLoaded);}

解决方案

这个答案可能会迟到,但我看到这篇文章并使用 pxpgraphics 评论得到了解决方案.对于那些需要使缓存无效的人,这将做到.请注意,此代码示例从 RSS 提要中提取其他数据;根据您的需要进行修改.

google.load("feeds", "1");函数初始化(){var feedLocation = "http://feeds.bbci.co.uk/news/rss.xml";/* 使用 randomNum 和 time 使 Google 缓存无效并获取最新文章.*/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(功能(结果){如果(!结果.错误){var container = document.getElementById("feed");for (var i = 0; i 

此代码假定您在页面上有此 DIV:

I am using the Google Feed JSAPI to read/parse a feed. The problem is, when the feed changes, the previous entries become invalid (links and images don't work) so I cannot load a cached version of the feed. I thought there would be an option when loading the feed to not use the cached version but I don't see one. My solution is to do add in a variable (t) to the end of the feed url so it is "unique" but this seems hacky (but it works). Anyone know of a better way to do it?

    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);
    }

解决方案

This answer might be coming in late, but I came across this post and got the solution using pxpgraphics comment. For those who need to invalidate the cache, this will do it. Note that this code example is pulling other data from the RSS feed; modify for your needs.

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);

This code assumes you have this DIV on the page:

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

这篇关于阻止 Google Feed API 使用缓存的 Feed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆