Skip to content

Commit 23497fa

Browse files
feat: add cache invalidation
1 parent b5d977c commit 23497fa

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

internal/server/cache.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package server
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67

78
"github.com/patrickmn/go-cache"
89
)
@@ -21,6 +22,10 @@ func (s *Server) getCacheKeyFromRequest(r *http.Request) cacheKey {
2122
return cacheKey(fmt.Sprintf("%s/%s:%s", cacheKeyPrefixRequest, r.Method, r.URL.EscapedPath()))
2223
}
2324

25+
func (s *Server) getCacheKeyPrefixFromPluginName(pluginName string) cacheKey {
26+
return cacheKey(fmt.Sprintf("%s/%s:/api/v2/plugins/%s", cacheKeyPrefixRequest, http.MethodGet, pluginName))
27+
}
28+
2429
func (s *Server) getCacheKeyWithPrefix(p cacheKeyPrefix, key string) cacheKey {
2530
return cacheKey(fmt.Sprintf("%s/%s", p, key))
2631
}
@@ -32,3 +37,26 @@ func (s *Server) getFromCache(k cacheKey) (any, bool) {
3237
func (s *Server) setInCache(k cacheKey, v any) {
3338
s.cache.Set(string(k), v, cache.DefaultExpiration)
3439
}
40+
41+
func (s *Server) invalidateByPrefix(prefix cacheKey) {
42+
for k := range s.cache.Items() {
43+
if strings.HasPrefix(k, string(prefix)) {
44+
s.cache.Delete(k)
45+
}
46+
}
47+
}
48+
49+
func (s *Server) cacheMiddleware(next http.Handler) http.Handler {
50+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
51+
if s.config.DisableRequestCache {
52+
next.ServeHTTP(w, r)
53+
return
54+
}
55+
if k, ok := s.getFromCache(s.getCacheKeyFromRequest(r)); ok {
56+
w.Header().Set("X-Go-Cache", "HIT")
57+
s.writeJSON(w, k)
58+
return
59+
}
60+
next.ServeHTTP(w, r)
61+
})
62+
}

internal/server/handler_batch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (s *Server) batchGetPlugins(w http.ResponseWriter, r *http.Request) {
147147
ContentType: aws.String("application/gzip"),
148148
Metadata: map[string]string{
149149
"checksum": tgzChecksum,
150+
"hash": batchResponse.DownloadHash,
150151
},
151152
})
152153
if closeErr := tarFile.Close(); closeErr != nil {

internal/server/handlers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func (s *Server) updateAllPlugins(w http.ResponseWriter, r *http.Request) {
2828
return
2929
}
3030
}
31+
s.invalidateByPrefix(s.getCacheKeyPrefixFromPluginName(""))
3132
s.writeJSON(w, map[string]bool{"ok": true})
3233
}
3334

@@ -46,6 +47,8 @@ func (s *Server) updatePlugin(w http.ResponseWriter, r *http.Request) {
4647
s.writeJSONError(w, r, http.StatusInternalServerError, err, "could not update plugin")
4748
return
4849
}
50+
51+
s.invalidateByPrefix(s.getCacheKeyPrefixFromPluginName(p.GetFullName()))
4952
s.writeJSON(w, map[string]bool{"ok": true})
5053
}
5154

internal/server/middleware.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,3 @@ func (s *Server) recoverMiddleware(next http.Handler) http.Handler {
3939
next.ServeHTTP(w, r)
4040
})
4141
}
42-
43-
func (s *Server) cacheMiddleware(next http.Handler) http.Handler {
44-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45-
if s.config.DisableRequestCache {
46-
next.ServeHTTP(w, r)
47-
return
48-
}
49-
if k, ok := s.getFromCache(s.getCacheKeyFromRequest(r)); ok {
50-
w.Header().Set("X-Cache", "HIT")
51-
s.writeJSON(w, k)
52-
return
53-
}
54-
next.ServeHTTP(w, r)
55-
})
56-
}

0 commit comments

Comments
 (0)