From 7ad3748a46e98aca7566e14fdda2c321bbad5cd5 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Wed, 14 Sep 2022 20:28:52 +0800 Subject: [PATCH] feat: update cache after remove instead of clear --- internal/op/fs.go | 33 +++++++++++++++++++++++++++++---- server/handles/fsmanage.go | 2 +- server/webdav/webdav.go | 2 +- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/internal/op/fs.go b/internal/op/fs.go index cf04cca0..88ffc554 100644 --- a/internal/op/fs.go +++ b/internal/op/fs.go @@ -27,6 +27,10 @@ func ClearCache(storage driver.Driver, path string) { listCache.Del(key) } +func Key(storage driver.Driver, path string) string { + return stdpath.Join(storage.GetStorage().MountPath, utils.StandardizePath(path)) +} + // List files in storage, not contains virtual file func List(ctx context.Context, storage driver.Driver, path string, args model.ListArgs, refresh ...bool) ([]model.Obj, error) { if storage.Config().CheckStatus && storage.GetStorage().Status != WORK { @@ -46,7 +50,7 @@ func List(ctx context.Context, storage driver.Driver, path string, args model.Li objs, err := storage.List(ctx, dir, args) return objs, errors.WithStack(err) } - key := stdpath.Join(storage.GetStorage().MountPath, path) + key := Key(storage, path) if len(refresh) == 0 || !refresh[0] { if files, ok := listCache.Get(key); ok { return files, nil @@ -266,7 +270,28 @@ func Remove(ctx context.Context, storage driver.Driver, path string) error { } return errors.WithMessage(err, "failed to get object") } - return errors.WithStack(storage.Remove(ctx, obj)) + err = storage.Remove(ctx, obj) + if err == nil { + key := Key(storage, stdpath.Dir(path)) + if objs, ok := listCache.Get(key); ok { + j := -1 + for i, m := range objs { + if m.GetName() == obj.GetName() { + j = i + break + } + } + if j >= 0 && j < len(objs) { + objs = append(objs[:j], objs[j+1:]...) + listCache.Set(key, objs) + } else { + log.Debugf("not found obj") + } + } else { + log.Debugf("not found parent cache") + } + } + return errors.WithStack(err) } func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file model.FileStreamer, up driver.UpdateProgress) error { @@ -317,8 +342,8 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file mod // set as complete up(100) // clear cache - key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath) - listCache.Del(key) + //key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath) + //listCache.Del(key) } return errors.WithStack(err) } diff --git a/server/handles/fsmanage.go b/server/handles/fsmanage.go index dc56e76c..a3c30e27 100644 --- a/server/handles/fsmanage.go +++ b/server/handles/fsmanage.go @@ -184,7 +184,7 @@ func FsRemove(c *gin.Context) { return } } - fs.ClearCache(req.Dir) + //fs.ClearCache(req.Dir) common.SuccessResp(c) } diff --git a/server/webdav/webdav.go b/server/webdav/webdav.go index e4d2c173..2c1ff1be 100644 --- a/server/webdav/webdav.go +++ b/server/webdav/webdav.go @@ -271,7 +271,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)) + //fs.ClearCache(path.Dir(reqPath)) return http.StatusNoContent, nil }