Skip to content

Commit

Permalink
Merge 2394641 into 69845bb
Browse files Browse the repository at this point in the history
  • Loading branch information
domano committed Jul 11, 2016
2 parents 69845bb + 2394641 commit 49767da
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
8 changes: 8 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func (c *Cache) Set(key string, label string, sizeBytes int, cacheObject interfa
}
}

func (c *Cache) Invalidate() {
c.lock.Lock()
defer c.lock.Unlock()
c.lruBackend.Purge()
c.currentSizeBytes = 0
return
}

// SizeByte returns the total memory consumption of the cache
func (c *Cache) SizeByte() int {
return int(atomic.LoadInt32(&c.currentSizeBytes))
Expand Down
19 changes: 19 additions & 0 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,22 @@ func Test_Cache_MaxBytes(t *testing.T) {
_, found = c.Get("d")
a.True(found)
}

func Test_Cache_Invalidation(t *testing.T) {
a := assert.New(t)

// given a cache of size 3
// with 3 entries
c := NewCache("my-cache", 3, 100, time.Hour)
c.Set("a", "", 0, "a")
c.Set("b", "", 0, "b")
c.Set("c", "", 0, "c")
a.Equal(3, c.Len())

//when i empty the cache
c.Invalidate()

//then the cache should be empty
assert.True(t, c.currentSizeBytes == 0)
assert.True(t, c.Len() == 0)
}
28 changes: 28 additions & 0 deletions composition/cache_invalidation_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package composition

import (
"github.com/tarent/lib-compose/logging"
"net/http"
"strings"
)

type CacheInvalidationHandler struct {
cache Cache
next http.Handler
}

func (cih *CacheInvalidationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" &&
strings.Contains(r.URL.EscapedPath(), "internal/cache") &&
cih.cache != nil {
logging.Application(r.Header).Info("cache was invalidated")
cih.cache.Invalidate()
}
if cih.next != nil {
cih.next.ServeHTTP(w, r)
}
}

func NewCacheInvalidationHandler(cache Cache, next http.Handler) *CacheInvalidationHandler {
return &CacheInvalidationHandler{cache: cache, next: next}
}
38 changes: 38 additions & 0 deletions composition/cache_invalidation_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package composition

import (
"github.com/golang/mock/gomock"
mockhttp "github.com/tarent/lib-compose/composition/mocks/net/http"
"net/http"
"testing"
)

func Test_CacheInvalidationHandler_Invalidation(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

//given
cacheMocK := NewMockCache(ctrl)
cih := NewCacheInvalidationHandler(cacheMocK, nil)
request, _ := http.NewRequest(http.MethodDelete, "internal/cache", nil)

//when
cacheMocK.EXPECT().Invalidate().Times(1)
cih.ServeHTTP(nil, request)
}

func Test_CacheInvalidationHandler_Delegate_Is_Called(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

//given
handlerMock := mockhttp.NewMockHandler(ctrl)
cacheMocK := NewMockCache(ctrl)
cih := NewCacheInvalidationHandler(cacheMocK, handlerMock)
request, _ := http.NewRequest(http.MethodDelete, "internal/cache", nil)

//when
cacheMocK.EXPECT().Invalidate().AnyTimes()
handlerMock.EXPECT().ServeHTTP(gomock.Any(), gomock.Any()).Times(1)
cih.ServeHTTP(nil, request)
}
18 changes: 9 additions & 9 deletions composition/interface_mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package composition

import (
gomock "github.com/golang/mock/gomock"

io "io"

http "net/http"
)

Expand Down Expand Up @@ -194,14 +194,6 @@ func (_mr *_MockContentRecorder) RequiredContent() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "RequiredContent")
}

func (_m *MockContent) SetReader(_param0 io.ReadCloser) {
_m.ctrl.Call(_m, "SetReader", _param0)
}

func (_mr *_MockContentRecorder) SetReader(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "SetReader", arg0)
}

func (_m *MockContent) Tail() Fragment {
ret := _m.ctrl.Call(_m, "Tail")
ret0, _ := ret[0].(Fragment)
Expand Down Expand Up @@ -356,6 +348,14 @@ func (_mr *_MockCacheRecorder) Get(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Get", arg0)
}

func (_m *MockCache) Invalidate() {
_m.ctrl.Call(_m, "Invalidate")
}

func (_mr *_MockCacheRecorder) Invalidate() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Invalidate")
}

func (_m *MockCache) Set(_param0 string, _param1 string, _param2 int, _param3 interface{}) {
_m.ctrl.Call(_m, "Set", _param0, _param1, _param2, _param3)
}
Expand Down
1 change: 1 addition & 0 deletions composition/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ type ErrorHandler interface {
type Cache interface {
Get(hash string) (cacheObject interface{}, found bool)
Set(hash string, label string, memorySize int, cacheObject interface{})
Invalidate()
}

0 comments on commit 49767da

Please sign in to comment.