Skip to content

Commit

Permalink
Support disabling the Item.ID lookup cache.
Browse files Browse the repository at this point in the history
The default remains caching enabled.
  • Loading branch information
jonbuffington committed Dec 21, 2013
1 parent c617a19 commit e5aac1b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rss

var database *db
var disabled bool

func init() {
database = NewDB()
Expand All @@ -19,7 +20,9 @@ func (d *db) Run() {

for {
s = <-d.req
if _, ok := d.known[s]; ok {
if disabled {
d.res <- false
} else if _, ok := d.known[s]; ok {
d.res <- true
} else {
d.res <- false
Expand Down
46 changes: 46 additions & 0 deletions database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package rss

import (
"testing"
)

var fixture = "Item.ID"

func TestDatabaseEnabled(t *testing.T) {
if database.req <- fixture; <-database.res {
t.Error("Should have not found the fixture string.")
}
if database.req <- fixture; !<-database.res {
t.Error("Should have found the fixture string.")
}
}

func TestDatabaseDisabled(t *testing.T) {
CacheParsedItemIDs(false)

if database.req <- fixture; <-database.res {
t.Error("Should have not found the fixture string even though it was recorded.")
}

n := len(database.known)
if database.req <- "foo"; <-database.res || len(database.known) != n {
t.Error("Should not record a new entry.")
}
}

func TestDatabaseReenabled(t *testing.T) {
CacheParsedItemIDs(true)

if database.req <- fixture; !<-database.res {
t.Error("Should have found the fixture string again.")
}

n := len(database.known)
if database.req <- "foo"; <-database.res || len(database.known) != n+1 {
t.Error("Should record a new entry.")
}

if database.req <- "foo"; !<-database.res {
t.Error("Should find the new entry.")
}
}
8 changes: 8 additions & 0 deletions rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func Parse(data []byte) (*Feed, error) {
panic("Unreachable.")
}

// CacheParsedItemIDs enables or disable Item.ID caching when parsing feeds.
// Returns whether Item.ID were cached prior to function call.
func CacheParsedItemIDs(flag bool) (didCache bool) {
didCache = !disabled
disabled = !flag
return
}

// Fetch downloads and parses the RSS feed at the given URL
func Fetch(url string) (*Feed, error) {
resp, err := http.Get(url)
Expand Down

0 comments on commit e5aac1b

Please sign in to comment.