github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/baidupcs/cache.go (about)

     1  package baidupcs
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/qjfoidnh/BaiduPCS-Go/baidupcs/expires"
     6  	"github.com/qjfoidnh/BaiduPCS-Go/baidupcs/pcserror"
     7  	"time"
     8  )
     9  
    10  // deleteCache 删除含有 dirs 的缓存
    11  func (pcs *BaiduPCS) deleteCache(dirs []string) {
    12  	cache := pcs.cacheOpMap.LazyInitCachePoolOp(OperationFilesDirectoriesList)
    13  	for _, v := range dirs {
    14  		key := v + "_" + defaultOrderOptionsStr
    15  		_, ok := cache.Load(key)
    16  		if ok {
    17  			cache.Delete(key)
    18  		}
    19  	}
    20  }
    21  
    22  // CacheFilesDirectoriesList 缓存获取
    23  func (pcs *BaiduPCS) CacheFilesDirectoriesList(path string, options *OrderOptions) (fdl FileDirectoryList, pcsError pcserror.Error) {
    24  	data := pcs.cacheOpMap.CacheOperation(OperationFilesDirectoriesList, path+"_"+fmt.Sprint(options), func() expires.DataExpires {
    25  		fdl, pcsError = pcs.FilesDirectoriesList(path, options)
    26  		if pcsError != nil {
    27  			return nil
    28  		}
    29  		return expires.NewDataExpires(fdl, 1*time.Minute)
    30  	})
    31  	if pcsError != nil {
    32  		return
    33  	}
    34  	return data.Data().(FileDirectoryList), nil
    35  }