Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extra endpoint for clear cache #366

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions server/cache_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ func cacheIndexHandler() http.Handler {
})
}

func cacheClearHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
clearCache(w, r)
})
}

func clearCache(w http.ResponseWriter, r *http.Request) {
if err := cache.Reset(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("internal cache error: %s", err)
}
log.Println("cache is successfully cleared")
w.WriteHeader(http.StatusOK)
}

// handles get requests.
func getCacheHandler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Path[len(cachePath):]
Expand Down
7 changes: 4 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const (
apiBasePath = "/api/" + apiVersion + "/"

// path to cache.
cachePath = apiBasePath + "cache/"
statsPath = apiBasePath + "stats"

cachePath = apiBasePath + "cache/"
statsPath = apiBasePath + "stats"
cacheClearPath = apiBasePath + "cache/clear"
// server version.
version = "1.0.0"
)
Expand Down Expand Up @@ -76,6 +76,7 @@ func main() {
logger.Print("cache initialised.")

// let the middleware log.
http.Handle(cacheClearPath, serviceLoader(cacheClearHandler(), requestMetrics(logger)))
http.Handle(cachePath, serviceLoader(cacheIndexHandler(), requestMetrics(logger)))
http.Handle(statsPath, serviceLoader(statsIndexHandler(), requestMetrics(logger)))

Expand Down
22 changes: 22 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ func TestDeleteKey(t *testing.T) {
}
}

func TestClearCache(t *testing.T) {
t.Parallel()

putRequest := httptest.NewRequest("PUT", testBaseString+"/api/v1/cache/putKey", bytes.NewBuffer([]byte("123")))
putResponseRecorder := httptest.NewRecorder()

putCacheHandler(putResponseRecorder, putRequest)

requestClear := httptest.NewRequest("DELETE", testBaseString+"/api/v1/cache/clear", nil)
rr := httptest.NewRecorder()

if err := cache.Set("testDeleteKey", []byte("123")); err != nil {
t.Errorf("can't set key for testing. %s", err)
}

clearCache(rr, requestClear)
resp := rr.Result()

if resp.StatusCode != 200 {
t.Errorf("want: 200; got: %d.\n\tcan't delete keys.", resp.StatusCode)
}
}
func TestGetStats(t *testing.T) {
t.Parallel()
var testStats bigcache.Stats
Expand Down
Loading