aliyunpan/internal/config/cache.go

56 lines
1.5 KiB
Go
Raw Normal View History

2021-10-30 22:53:08 +08:00
package config
import (
"github.com/tickstep/aliyunpan-api/aliyunpan"
"github.com/tickstep/aliyunpan-api/aliyunpan/apierror"
"github.com/tickstep/library-go/expires"
"path"
"time"
)
2021-10-31 15:53:27 +08:00
// DeleteCache 删除含有 dirs 的缓存
func (pu *PanUser) DeleteCache(dirs []string) {
2021-10-30 22:53:08 +08:00
cache := pu.cacheOpMap.LazyInitCachePoolOp(pu.ActiveDriveId)
for _, v := range dirs {
key := v + "_" + "OrderByName"
_, ok := cache.Load(key)
if ok {
cache.Delete(key)
}
}
}
2021-10-31 15:53:27 +08:00
// DeleteOneCache 删除缓存
func (pu *PanUser) DeleteOneCache(dirPath string) {
ps := []string{dirPath}
pu.DeleteCache(ps)
}
2021-10-30 22:53:08 +08:00
// CacheFilesDirectoriesList 缓存获取
func (pu *PanUser) CacheFilesDirectoriesList(pathStr string) (fdl aliyunpan.FileList, apiError *apierror.ApiError) {
data := pu.cacheOpMap.CacheOperation(pu.ActiveDriveId, pathStr+"_OrderByName", func() expires.DataExpires {
var fi *aliyunpan.FileEntity
fi, apiError = pu.panClient.OpenapiPanClient().FileInfoByPath(pu.ActiveDriveId, pathStr)
2021-10-30 22:53:08 +08:00
if apiError != nil {
return nil
}
fileListParam := &aliyunpan.FileListParam{
2022-07-02 16:24:11 +08:00
DriveId: pu.ActiveDriveId,
2021-10-30 22:53:08 +08:00
ParentFileId: fi.FileId,
}
fdl, apiError = pu.panClient.OpenapiPanClient().FileListGetAll(fileListParam, 200)
2021-10-30 22:53:08 +08:00
if apiError != nil {
return nil
}
// construct full path
for _, f := range fdl {
f.Path = path.Join(pathStr, f.FileName)
}
2021-12-30 20:24:33 +08:00
return expires.NewDataExpires(fdl, 10*time.Minute)
2021-10-30 22:53:08 +08:00
})
if apiError != nil {
return
}
return data.Data().(aliyunpan.FileList), nil
}