Implement invalidation through DELETE /cache/all (#8)

* Implement invalidation through `DELETE /cache/all`

* Fix review issues

* Fix review issues
This commit is contained in:
Roman Sharkov 2019-11-28 20:07:58 +01:00 committed by Frederik Löffert
parent d2fc43350c
commit 34ca1ca22a
3 changed files with 49 additions and 0 deletions

View File

@ -27,6 +27,11 @@ func (c *Cache) Len() int {
return counter
}
// GetAll returns all cached items
func (c *Cache) GetAll() ([]store.CacheItem, error) {
return c.store.GetAll()
}
func (c *Cache) GetAllEtags(workspace string) (etags map[string]string) {
return c.store.GetAllEtags(workspace)
}

View File

@ -109,6 +109,49 @@ func (p *Proxy) getContent(w http.ResponseWriter, r *http.Request) {
return
}
// invalidateCache will invalidate all cached contentserver export files
func (p *Proxy) invalidateCacheAll(w http.ResponseWriter, r *http.Request) {
// extract request data
workspace := strings.TrimSpace(
strings.ToLower(r.URL.Query().Get("workspace")),
)
user := r.Header.Get("X-User")
// validate workspace
if workspace == "" {
workspace = cms.WorkspaceLive
}
// logger
log := p.setupLogger(r, "invalidateCacheAll").WithFields(logrus.Fields{
logging.FieldWorkspace: workspace,
"user": user,
})
cachedItems, err := p.contentCache.GetAll()
if err != nil {
log.WithError(err).Error("couldn't get all cache items")
http.Error(
w,
http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError,
)
}
if len(p.config.Neos.Dimensions) == 0 {
log.Warn("no neos dimension configured")
}
for _, ci := range cachedItems {
p.contentCache.Invalidate(ci.ID, ci.Dimension, ci.Workspace)
}
w.WriteHeader(http.StatusAccepted)
log.
WithField("numInvalidationRequests", len(cachedItems)).
Debug("cache invalidation requests accepted")
}
// invalidateCache will invalidate cached contentserver export file
func (p *Proxy) invalidateCache(w http.ResponseWriter, r *http.Request) {

View File

@ -40,6 +40,7 @@ func (p *Proxy) setupRoutes() {
// neosproxy/cache/%s?workspace=%s
neosproxyRouter := p.router.PathPrefix(neosproxyPath).Subrouter()
neosproxyRouter.Use(p.middlewareTokenAuth)
neosproxyRouter.HandleFunc("/cache/all", p.invalidateCacheAll).Methods(http.MethodDelete)
neosproxyRouter.HandleFunc("/cache/{id}", p.invalidateCache).Methods(http.MethodDelete)
neosproxyRouter.HandleFunc("/cache/{id}", p.invalidateCache).Methods(http.MethodDelete).Queries("workspace", "{workspace}").Name("api-delete-cache")
neosproxyRouter.HandleFunc("/status", p.streamStatus).Methods(http.MethodGet)