From 53416172e7ddb0e58053c3ab1297ef45558e59aa Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 30 Jun 2022 22:51:49 +0800 Subject: [PATCH] feat: clear cache after change --- internal/fs/util.go | 9 +++++++++ internal/operations/fs.go | 5 +++++ server/controllers/fsmanage.go | 14 +++++++++++--- server/webdav/file.go | 3 +++ server/webdav/webdav.go | 3 +++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/internal/fs/util.go b/internal/fs/util.go index 4e328d6316f..883158aa339 100644 --- a/internal/fs/util.go +++ b/internal/fs/util.go @@ -1,6 +1,7 @@ package fs import ( + "github.com/alist-org/alist/v3/internal/operations" "io" "mime" "net/http" @@ -12,6 +13,14 @@ import ( "github.com/pkg/errors" ) +func ClearCache(path string) { + account, actualPath, err := operations.GetAccountAndActualPath(path) + if err != nil { + return + } + operations.ClearCache(account, actualPath) +} + func containsByName(files []model.Obj, file model.Obj) bool { for _, f := range files { if f.GetName() == file.GetName() { diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 60779238179..abeda730244 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -22,6 +22,11 @@ import ( var filesCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64)) var filesG singleflight.Group[[]model.Obj] +func ClearCache(account driver.Driver, path string) { + key := stdpath.Join(account.GetAccount().VirtualPath, path) + filesCache.Del(key) +} + // List files in storage, not contains virtual file func List(ctx context.Context, account driver.Driver, path string, refresh ...bool) ([]model.Obj, error) { path = utils.StandardizePath(path) diff --git a/server/controllers/fsmanage.go b/server/controllers/fsmanage.go index 36213578f6d..122a6f1f0b2 100644 --- a/server/controllers/fsmanage.go +++ b/server/controllers/fsmanage.go @@ -41,6 +41,7 @@ func FsMkdir(c *gin.Context) { common.ErrorResp(c, err, 500) return } + fs.ClearCache(stdpath.Dir(req.Path)) common.SuccessResp(c) } @@ -81,6 +82,8 @@ func FsMove(c *gin.Context) { return } } + fs.ClearCache(req.SrcDir) + fs.ClearCache(req.DstDir) common.SuccessResp(c) } @@ -112,6 +115,9 @@ func FsCopy(c *gin.Context) { return } } + if len(req.Names) != len(addedTask) { + fs.ClearCache(req.DstDir) + } if len(addedTask) > 0 { common.SuccessResp(c, fmt.Sprintf("Added %d tasks", len(addedTask))) } else { @@ -140,11 +146,12 @@ func FsRename(c *gin.Context) { common.ErrorResp(c, err, 500) return } + fs.ClearCache(stdpath.Dir(req.Path)) common.SuccessResp(c) } type RemoveReq struct { - Path string `json:"path"` + Dir string `json:"dir"` Names []string `json:"names"` } @@ -163,14 +170,15 @@ func FsRemove(c *gin.Context) { common.ErrorResp(c, errs.PermissionDenied, 403) return } - req.Path = stdpath.Join(user.BasePath, req.Path) + req.Dir = stdpath.Join(user.BasePath, req.Dir) for _, name := range req.Names { - err := fs.Remove(c, stdpath.Join(req.Path, name)) + err := fs.Remove(c, stdpath.Join(req.Dir, name)) if err != nil { common.ErrorResp(c, err, 500) return } } + fs.ClearCache(req.Dir) common.SuccessResp(c) } diff --git a/server/webdav/file.go b/server/webdav/file.go index 0c33656d9de..8b021c1d996 100644 --- a/server/webdav/file.go +++ b/server/webdav/file.go @@ -31,6 +31,8 @@ func moveFiles(ctx context.Context, src, dst string, overwrite bool) (status int if err != nil { return http.StatusInternalServerError, err } + fs.ClearCache(path.Dir(src)) + fs.ClearCache(path.Dir(dst)) // TODO if there are no files copy, should return 204 return http.StatusCreated, nil } @@ -43,6 +45,7 @@ func copyFiles(ctx context.Context, src, dst string, overwrite bool) (status int if err != nil { return http.StatusInternalServerError, err } + fs.ClearCache(path.Dir(dst)) // TODO if there are no files copy, should return 204 return http.StatusCreated, nil } diff --git a/server/webdav/webdav.go b/server/webdav/webdav.go index 1302a3dbcbc..138e5db1da8 100644 --- a/server/webdav/webdav.go +++ b/server/webdav/webdav.go @@ -267,6 +267,7 @@ func (h *Handler) handleDelete(w http.ResponseWriter, r *http.Request) (status i if err := fs.Remove(ctx, reqPath); err != nil { return http.StatusMethodNotAllowed, err } + fs.ClearCache(path.Dir(reqPath)) return http.StatusNoContent, nil } @@ -311,6 +312,7 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int, return http.StatusInternalServerError, err } w.Header().Set("ETag", etag) + fs.ClearCache(path.Dir(reqPath)) return http.StatusCreated, nil } @@ -338,6 +340,7 @@ func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request) (status in } return http.StatusMethodNotAllowed, err } + fs.ClearCache(path.Dir(reqPath)) return http.StatusCreated, nil }