From 110a94b207f2c84cc067af75285a7e7cdb0c48ea Mon Sep 17 00:00:00 2001 From: Michael Handler Date: Sun, 1 Mar 2015 22:41:15 -0800 Subject: [PATCH] Add IgnoreCacheOnce() to Feed objects. When called, the Feed object will ignore any cached data or refresh hint metadata, and always attempt to download the feed data from the source. Resets to normal (cache-abiding) behavior after a successful retrieval. --- feed.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/feed.go b/feed.go index 1425dfe..26d134e 100644 --- a/feed.go +++ b/feed.go @@ -27,11 +27,12 @@ package feeder import ( "fmt" - xmlx "github.com/jteeuwen/go-pkg-xmlx" "net/http" "strconv" "strings" "time" + + xmlx "github.com/jteeuwen/go-pkg-xmlx" ) type UnsupportedFeedError struct { @@ -99,6 +100,10 @@ type Feed struct { // Last time content was fetched. Used in conjunction with CacheTimeout // to ensure we don't get content too often. lastupdate time.Time + + // On our next fetch *ONLY* (this will get reset to false afterwards), + // ignore all cache settings and update frequency hints, and always fetch. + ignoreCacheOnce bool } // New is a helper function to stay semi-compatible with @@ -129,6 +134,13 @@ func (this *Feed) LastUpdate() time.Time { return this.lastupdate } +// Until the next *successful* fetching of the feed's content, the +// fetcher will ignore all cache values and update interval hints, +// and always attempt to retrieve a fresh copy of the feed. +func (this *Feed) IgnoreCacheOnce() { + this.ignoreCacheOnce = true +} + // Fetch retrieves the feed's latest content if necessary. // // The charset parameter overrides the xml decoder's CharsetReader. @@ -163,7 +175,12 @@ func (this *Feed) FetchClient(uri string, client *http.Client, charset xmlx.Char return } - return this.makeFeed(doc) + if err = this.makeFeed(doc); err == nil { + // Only if fetching and parsing succeeded. + this.ignoreCacheOnce = false + } + + return } // Fetch retrieves the feed's content from the []byte @@ -225,6 +242,13 @@ func (this *Feed) notifyListeners() { // true). If this function returns true, you can be sure that a fresh feed // update will be performed. func (this *Feed) CanUpdate() bool { + if this.ignoreCacheOnce { + // Even though ignoreCacheOnce is only good for one fetch, we only reset + // it after a successful fetch, so CanUpdate() has no side-effects, and + // can be called repeatedly before performing the actual fetch. + return true + } + // Make sure we are not within the specified cache-limit. // This ensures we don't request data too often. if this.SecondsTillUpdate() > 0 {