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

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

问题描述

我正在使用 Google Feed JSAPI 来读取/解析Feed。问题是,当订阅源发生变化时,以前的条目变得无效(链接和图像不起作用),因此我无法加载订阅源的缓存版本。我认为当加载Feed不使用缓存版本时会有一个选项,但我没有看到它。我的解决方案是添加一个变量(t)到feed url的末尾,所以它是独一无二的,但这看起来很乱(但有效)。任何人都知道更好的方式来做到这一点?

 函数onLoad(){
//创建一个feed实例将抓住饲料饲料。
var feed = new google.feeds.Feed(feedLocation +& t =+ new Date()。getTime());

//以XML格式请求结果(以便我们可以解析出所有信息
feed.setResultFormat(google.feeds.Feed.XML_FORMAT);

//必须设置 - 如果你不这样做,它默认为4
feed.setNumEntries(50);

//呼叫加载发送请求,它需要一个回调函数。
feed.load(feedLoaded);
}


解决方案这个答案可能会在晚些时候发布,但我遇到了这个帖子,并且使用 pxpgraphics 注释得到了解决方案。对于那些需要使缓存无效的人,这个请注意,此代码示例从RSS提要中提取其他数据;根据您的需要进行修改。

  google.load (feeds,1); 

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

/ *使用randomNum和时间使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(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> 


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天全站免登陆