Skip to content

Commit

Permalink
feat: clear cache after change
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 30, 2022
1 parent 2b17266 commit 5341617
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
9 changes: 9 additions & 0 deletions internal/fs/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fs

import (
"github.com/alist-org/alist/v3/internal/operations"
"io"
"mime"
"net/http"
Expand All @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions server/controllers/fsmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func FsMkdir(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
fs.ClearCache(stdpath.Dir(req.Path))
common.SuccessResp(c)
}

Expand Down Expand Up @@ -81,6 +82,8 @@ func FsMove(c *gin.Context) {
return
}
}
fs.ClearCache(req.SrcDir)
fs.ClearCache(req.DstDir)
common.SuccessResp(c)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
}

Expand All @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions server/webdav/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions server/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 5341617

Please sign in to comment.