Skip to content

Commit 49fdc70

Browse files
feat: add invalidate cache handler
1 parent cf9a0eb commit 49fdc70

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

internal/server/cache.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ func (s *Server) setInCache(ctx context.Context, k cacheKey, v any, expiration .
5757
s.cache.Set(strKey, v, exp)
5858
}
5959

60-
func (s *Server) invalidateByPrefix(prefix cacheKey) {
60+
func (s *Server) invalidateByPrefix(prefix cacheKey) int {
61+
cnt := 0
6162
for k := range s.cache.Items() {
6263
if strings.HasPrefix(k, string(prefix)) {
6364
s.cache.Delete(k)
65+
cnt++
6466
}
6567
}
68+
return cnt
6669
}
6770

6871
func (s *Server) cacheMiddleware(next http.Handler) http.Handler {

internal/server/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ func (s *Server) indexHandler(w http.ResponseWriter, r *http.Request) {
5050
})
5151
}
5252

53+
func (s *Server) invalidateCacheHandler(w http.ResponseWriter, r *http.Request) {
54+
prefix := cacheKey(r.URL.Query().Get("prefix"))
55+
deleted := s.invalidateByPrefix(prefix)
56+
s.writeJSON(w, map[string]any{
57+
"prefix": prefix,
58+
"deleted": deleted,
59+
})
60+
}
61+
5362
func (s *Server) apiV2Routes(r chi.Router) {
5463
r.Route("/plugins", func(r chi.Router) {
5564
r.With(s.cacheMiddleware).Group(func(r chi.Router) {
@@ -66,6 +75,7 @@ func (s *Server) apiV2Routes(r chi.Router) {
6675
r.Put("/", s.updateAllPlugins)
6776
r.Put("/{plugin}", s.updatePlugin)
6877
r.Put("/{plugin}/versions/{version}", s.updatePlugin)
78+
r.Delete("/_cache", s.invalidateCacheHandler)
6979
})
7080
})
7181
}

0 commit comments

Comments
 (0)