Fix: UI not refreshing after ZFS pool operations due to caching

This commit is contained in:
2025-12-16 19:58:52 +07:00
parent e1a66dc7df
commit 0c70777181
4 changed files with 21 additions and 5 deletions

3
.gitignore vendored
View File

@@ -16,3 +16,6 @@ pluto-api
# Runtime
*.log
# Temporary vdevs
data/vdevs/

View File

@@ -1,6 +1,8 @@
package httpapp
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"log"
@@ -76,6 +78,12 @@ func (a *App) handleCreatePool(w http.ResponseWriter, r *http.Request) {
return
}
// Invalidate cache for GET /api/v1/pools
keyToInvalidate := "GET:/api/v1/pools"
hash := sha256.Sum256([]byte(keyToInvalidate))
cacheKey := hex.EncodeToString(hash[:])
a.cache.Invalidate(cacheKey)
pool, err := a.zfs.GetPool(req.Name)
if err != nil {
writeJSON(w, http.StatusCreated, map[string]string{"message": "pool created", "name": req.Name})
@@ -114,6 +122,12 @@ func (a *App) handleDeletePool(w http.ResponseWriter, r *http.Request) {
return
}
// Invalidate cache for GET /api/v1/pools
keyToInvalidate := "GET:/api/v1/pools"
hash := sha256.Sum256([]byte(keyToInvalidate))
cacheKey := hex.EncodeToString(hash[:])
a.cache.Invalidate(cacheKey)
writeJSON(w, http.StatusOK, map[string]string{"message": "pool destroyed", "name": name})
}

View File

@@ -52,6 +52,7 @@ type App struct {
backupService *backup.Service
maintenanceService *maintenance.Service
tlsConfig *tls.Config
cache *ResponseCache
}
func New(cfg Config) (*App, error) {
@@ -175,6 +176,7 @@ func New(cfg Config) (*App, error) {
backupService: backupService,
maintenanceService: maintenanceService,
tlsConfig: tlsConfig,
cache: NewResponseCache(5 * time.Minute),
}
// Start snapshot scheduler (runs every 15 minutes)

View File

@@ -126,9 +126,6 @@ func generateETag(content []byte) string {
// cacheMiddleware provides response caching for GET requests
func (a *App) cacheMiddleware(next http.Handler) http.Handler {
// Default TTL: 5 minutes for GET requests
cache := NewResponseCache(5 * time.Minute)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Only cache GET requests
if r.Method != http.MethodGet {
@@ -154,7 +151,7 @@ func (a *App) cacheMiddleware(next http.Handler) http.Handler {
// Check cache
cacheKey := generateCacheKey(r)
if entry, found := cache.Get(cacheKey); found {
if entry, found := a.cache.Get(cacheKey); found {
// Check If-None-Match header for ETag validation
ifNoneMatch := r.Header.Get("If-None-Match")
if ifNoneMatch == entry.ETag {
@@ -202,7 +199,7 @@ func (a *App) cacheMiddleware(next http.Handler) http.Handler {
ETag: etag,
}
cache.Set(cacheKey, entry)
a.cache.Set(cacheKey, entry)
// Add cache headers
w.Header().Set("ETag", etag)