Skip to content

Commit

Permalink
feat: update cache after remove instead of clear
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 14, 2022
1 parent 66b2562 commit 7ad3748
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
33 changes: 29 additions & 4 deletions internal/op/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion server/handles/fsmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func FsRemove(c *gin.Context) {
return
}
}
fs.ClearCache(req.Dir)
//fs.ClearCache(req.Dir)
common.SuccessResp(c)
}

Expand Down
2 changes: 1 addition & 1 deletion server/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 7ad3748

Please sign in to comment.