refactor: Optimize download cache

This commit is contained in:
banbxio 2025-03-27 23:43:36 +08:00
parent 63ad80b0bc
commit 289f88867b

View File

@ -40,7 +40,6 @@ func NewDownloadTask(url, filename, pathname string, downloaderCache *cache.Cach
Pathname: pathname, Pathname: pathname,
cache: downloaderCache, cache: downloaderCache,
} }
downloaderCache.Set(taskId.String(), downloadTask, cache.NoExpiration)
return downloadTask return downloadTask
} }
@ -52,9 +51,6 @@ func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p) n := len(p)
wc.task.savedSize += int64(n) wc.task.savedSize += int64(n)
wc.task.cache.Set(wc.task.TaskID.String(), wc.task, cache.NoExpiration) wc.task.cache.Set(wc.task.TaskID.String(), wc.task, cache.NoExpiration)
if n == 0 {
wc.task.cache.Delete(wc.task.TaskID.String())
}
return n, nil return n, nil
} }
@ -117,6 +113,7 @@ func downloadStatusHandler(downloaderCache *cache.Cache) handleFunc {
} }
func downloadWithTask(fs afero.Fs, task *DownloadTask) error { func downloadWithTask(fs afero.Fs, task *DownloadTask) error {
task.cache.Set(task.TaskID.String(), task, cache.NoExpiration)
err := fs.MkdirAll(task.Pathname, files.PermDir) err := fs.MkdirAll(task.Pathname, files.PermDir)
if err != nil { if err != nil {
return err return err