refactor: split the db package hook and cache to the op package (#2747)
* refactor:separate the setting method from the db package to the op package and add the cache * refactor:separate the meta method from the db package to the op package * fix:setting not load database data * refactor:separate the user method from the db package to the op package * refactor:remove user JoinPath error * fix:op package user cache * refactor:fs package list method * fix:tile virtual paths (close #2743) * Revert "refactor:remove user JoinPath error" This reverts commit 4e20daaf9e700da047000d4fd4900abbe05c3848. * clean path directly may lead to unknown behavior * fix: The path of the meta passed in must be prefix of reqPath * chore: rename all virtualPath to mountPath * fix: `getStoragesByPath` and `GetStorageVirtualFilesByPath` is_sub_path: /a/b isn't subpath of /a/bc * fix: don't save setting if hook error Co-authored-by: Noah Hsu <i@nn.ci>
This commit is contained in:
88
internal/op/meta.go
Normal file
88
internal/op/meta.go
Normal file
@ -0,0 +1,88 @@
|
||||
package op
|
||||
|
||||
import (
|
||||
stdpath "path"
|
||||
"time"
|
||||
|
||||
"github.com/Xhofe/go-cache"
|
||||
"github.com/alist-org/alist/v3/internal/db"
|
||||
"github.com/alist-org/alist/v3/internal/errs"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/pkg/singleflight"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var metaCache = cache.NewMemCache(cache.WithShards[*model.Meta](2))
|
||||
|
||||
// metaG maybe not needed
|
||||
var metaG singleflight.Group[*model.Meta]
|
||||
|
||||
func GetNearestMeta(path string) (*model.Meta, error) {
|
||||
return getNearestMeta(utils.FixAndCleanPath(path))
|
||||
}
|
||||
func getNearestMeta(path string) (*model.Meta, error) {
|
||||
meta, err := GetMetaByPath(path)
|
||||
if err == nil {
|
||||
return meta, nil
|
||||
}
|
||||
if errors.Cause(err) != gorm.ErrRecordNotFound {
|
||||
return nil, err
|
||||
}
|
||||
if path == "/" {
|
||||
return nil, errs.MetaNotFound
|
||||
}
|
||||
return getNearestMeta(stdpath.Dir(path))
|
||||
}
|
||||
|
||||
func GetMetaByPath(path string) (*model.Meta, error) {
|
||||
return getMetaByPath(utils.FixAndCleanPath(path))
|
||||
}
|
||||
func getMetaByPath(path string) (*model.Meta, error) {
|
||||
meta, ok := metaCache.Get(path)
|
||||
if ok {
|
||||
return meta, nil
|
||||
}
|
||||
meta, err, _ := metaG.Do(path, func() (*model.Meta, error) {
|
||||
_meta, err := db.GetMetaByPath(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
metaCache.Set(path, _meta, cache.WithEx[*model.Meta](time.Hour))
|
||||
return _meta, nil
|
||||
})
|
||||
return meta, err
|
||||
}
|
||||
|
||||
func DeleteMetaById(id uint) error {
|
||||
old, err := db.GetMetaById(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metaCache.Del(old.Path)
|
||||
return db.DeleteMetaById(id)
|
||||
}
|
||||
|
||||
func UpdateMeta(u *model.Meta) error {
|
||||
u.Path = utils.FixAndCleanPath(u.Path)
|
||||
old, err := db.GetMetaById(u.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metaCache.Del(old.Path)
|
||||
return db.UpdateMeta(u)
|
||||
}
|
||||
|
||||
func CreateMeta(u *model.Meta) error {
|
||||
u.Path = utils.FixAndCleanPath(u.Path)
|
||||
return db.CreateMeta(u)
|
||||
}
|
||||
|
||||
func GetMetaById(id uint) (*model.Meta, error) {
|
||||
return db.GetMetaById(id)
|
||||
}
|
||||
|
||||
func GetMetas(pageIndex, pageSize int) (metas []model.Meta, count int64, err error) {
|
||||
return db.GetMetas(pageIndex, pageSize)
|
||||
}
|
Reference in New Issue
Block a user