2012-11-15 177 views
9

我使用Google Feed JSAPI讀/解析飼料。問題是,當飼料的變化,以前的條目變得無效(鏈接和圖像不工作),所以我不能負載飼料的緩存版本。我認爲當加載Feed不使用緩存版本時會有一個選項,但我沒有看到它。我的解決方案是添加一個變量(t)到feed url的末尾,所以它是「獨一無二的」,但這看起來很亂(但有效)。任何人都知道更好的方法來做到這一點?從防止谷歌飼料API使用緩存飼料

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

你找到一個解決辦法?這裏的問題在於,提供Feed的服務器正在使用基於Feed的URL的緩存。因此,如果我將隨機字符串添加到該URL的查詢字符串中,那麼這不僅會繞過Google的緩存,還會繞過服務器的緩存。猜猜我必須修改服務器以添加一個特殊的*旁路外部緩存*參數,這在進行內部緩存時會被忽略。 – feklee

+0

不,除了我在問題中提到的問題外,沒有其他解決方案 - 將時間添加爲「隨機」參數。它工作,只是沒有我想要的那麼幹淨。 –

+6

我現在做同樣的,也將是這樣的:「?bypass_cache =」'+ Math.floor(Date.now()/ refreshIntervalInMs)' – feklee

回答

0

嘗試feed.includeHistoricalEntries();它可能會解決您的問題。

+0

(順便提一下,保持Vogon詩歌!) –

+0

從API文檔看來,這聽起來並不像我所需要的那樣。事實上,這聽起來像我想dontIncludeHistoricalEntries()。我會試一試,看看。 RE:Vogon詩歌 - 我會的! :) http://myvogonpoetry.com是我的博客:) –

+1

噢好吧,對不起。所以,你想「無效」緩存。也許問一個隨機參數的URL? –

2

這個答案可能會晚來,但是我遇到了這個職位,並得到使用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> 
+0

這是行得通的,但它並沒有比我用「new Date()。getTime()」所做的更好。我希望有一些更優雅的東西,它不需要在「隨機」參數上加固,而是實際上使用了緩存。到目前爲止,@feklee提出的使用refreshIntervalInMs的解決方案最接近我想要的。 –

+0

你說得對。我希望有一種內置的方式告訴Google不要緩存它。也許他們更喜歡這種方式,因爲它可能需要較少的服務器工作。 – Alex