-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters