Merge pull request #60 from handlerbot/master

Add IgnoreCacheOnce() to Feed objects.
This commit is contained in:
Jim Teeuwen 2015-03-02 11:58:50 +01:00
commit 2382fc0262
1 changed files with 26 additions and 2 deletions

28
feed.go
View File

@ -27,11 +27,12 @@ package feeder
import ( import (
"fmt" "fmt"
xmlx "github.com/jteeuwen/go-pkg-xmlx"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time" "time"
xmlx "github.com/jteeuwen/go-pkg-xmlx"
) )
type UnsupportedFeedError struct { type UnsupportedFeedError struct {
@ -99,6 +100,10 @@ type Feed struct {
// Last time content was fetched. Used in conjunction with CacheTimeout // Last time content was fetched. Used in conjunction with CacheTimeout
// to ensure we don't get content too often. // to ensure we don't get content too often.
lastupdate time.Time 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 // New is a helper function to stay semi-compatible with
@ -129,6 +134,13 @@ func (this *Feed) LastUpdate() time.Time {
return this.lastupdate 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. // Fetch retrieves the feed's latest content if necessary.
// //
// The charset parameter overrides the xml decoder's CharsetReader. // 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
} }
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 // 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 // true). If this function returns true, you can be sure that a fresh feed
// update will be performed. // update will be performed.
func (this *Feed) CanUpdate() bool { 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. // Make sure we are not within the specified cache-limit.
// This ensures we don't request data too often. // This ensures we don't request data too often.
if this.SecondsTillUpdate() > 0 { if this.SecondsTillUpdate() > 0 {