Skip to content

Commit

Permalink
Merge pull request #17 from tarent/no-post-responses-from-cache
Browse files Browse the repository at this point in the history
do not fetch results out of the cache if it is no GET request
  • Loading branch information
domano authored Jul 11, 2016
2 parents b503d54 + 5039b0f commit 456a5da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
8 changes: 5 additions & 3 deletions composition/cache_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ func NewCachingContentLoader(cache Cache) *CachingContentLoader {
func (loader *CachingContentLoader) Load(fd *FetchDefinition) (Content, error) {
hash := fd.Hash()

if cFromCache, exist := loader.cache.Get(hash); exist {
logging.Cacheinfo(fd.URL, true)
return cFromCache.(Content), nil
if fd.Method == "GET" {
if cFromCache, exist := loader.cache.Get(hash); exist {
logging.Cacheinfo(fd.URL, true)
return cFromCache.(Content), nil
}
}
logging.Cacheinfo(fd.URL, false)
c, err := loader.load(fd)
Expand Down
27 changes: 26 additions & 1 deletion composition/cache_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ func Test_CacheLoader_Found(t *testing.T) {
a.Equal(c, result)
}

func Test_CacheLoader_NoLookupForPostRequests(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := assert.New(t)

// given:
fd := NewFetchDefinition("/foo")
fd.Method = "POST"
c := NewMemoryContent()
c.url = "/foo"

// and a cache returning nothing
cacheMocK := NewMockCache(ctrl)
httpLoaderMocK := NewMockContentLoader(ctrl)
httpLoaderMocK.EXPECT().Load(gomock.Any()).Return(c, nil)

// when: we load the object
loader := NewCachingContentLoader(cacheMocK)
loader.httpContentLoader = httpLoaderMocK

// it is returned
result, err := loader.Load(fd)
a.NoError(err)
a.Equal(c, result)
}

func Test_CacheLoader_NotFound(t *testing.T) {
tests := []struct {
url string
Expand All @@ -41,7 +67,6 @@ func Test_CacheLoader_NotFound(t *testing.T) {
}{
{"http://example.de", "GET", true},
{"file:///some/file", "GET", true},
{"http://example.de", "POST", false},
}
for _, test := range tests {
ctrl := gomock.NewController(t)
Expand Down

0 comments on commit 456a5da

Please sign in to comment.