Skip to content

Commit

Permalink
add TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
Siva Manivannan committed Jun 26, 2024
1 parent e78ff0e commit 33576b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Start(params APIServerParams) {
r.HandleFunc("/api/v1/app/info", handlers.GetCurrentAppInfo).Methods("GET")
r.HandleFunc("/api/v1/app/updates", handlers.GetAppUpdates).Methods("GET")
r.HandleFunc("/api/v1/app/history", handlers.GetAppHistory).Methods("GET")
r.HandleFunc("/api/v1/app/custom-metrics", handlers.SendCustomAppMetrics).Methods("POST", "PATCH")
r.HandleFunc("/api/v1/app/custom-metrics", handlers.CacheMiddleware(handlers.SendCustomAppMetrics, 1*time.Minute)).Methods("POST", "PATCH")
r.HandleFunc("/api/v1/app/custom-metrics/{key}", handlers.DeleteCustomAppMetricsKey).Methods("DELETE")
r.HandleFunc("/api/v1/app/instance-tags", handlers.SendAppInstanceTags).Methods("POST")

Expand Down
17 changes: 10 additions & 7 deletions pkg/handlers/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func RequireValidLicenseIDMiddleware(next http.Handler) http.Handler {
type CacheEntry struct {
RequestBody []byte
ResponseBody []byte
StatusCode int
Expiry time.Time
}

Expand Down Expand Up @@ -104,7 +105,8 @@ func (r *responseRecorder) Write(b []byte) (int, error) {
return r.ResponseWriter.Write(b)
}

func CacheMiddleware(next http.Handler, duration time.Duration, statusCode int) http.HandlerFunc {
func CacheMiddleware(next http.HandlerFunc, duration time.Duration) http.HandlerFunc {
// Each handler has its own cache to reduce contention
cache := NewCache()

return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -118,18 +120,19 @@ func CacheMiddleware(next http.Handler, duration time.Duration, statusCode int)

r.Body = io.NopCloser(bytes.NewBuffer(body))

cacheKey := r.Method + "::" + r.URL.Path + "::" + string(body)
key := r.Method + "::" + r.URL.Path + "::" + string(body)

if entry, found := cache.Get(cacheKey); found {
logger.Infof("cache middleware: serving from cache for method %s path %s", r.Method, r.URL.Path)
JSON(w, statusCode, entry.ResponseBody)
if entry, found := cache.Get(key); found {
logger.Infof("cache middleware: serving from cache for method: %s path: %s ttl: %s ", r.Method, r.URL.Path, entry.Expiry.Sub(time.Now()).String())
JSONCached(w, entry.StatusCode, entry.ResponseBody)
return
}

recorder := &responseRecorder{ResponseWriter: w, Body: &bytes.Buffer{}}
next.ServeHTTP(recorder, r)
next(recorder, r)

cache.Set(cacheKey, CacheEntry{
cache.Set(key, CacheEntry{
StatusCode: recorder.statusCode,
RequestBody: body,
ResponseBody: recorder.Body.Bytes(),
}, duration)
Expand Down

0 comments on commit 33576b3

Please sign in to comment.