Skip to content

Commit c98b441

Browse files
committed
chore: 优化分片上传
- 超时时间默认24小时 - 加速分片记录的删除
1 parent e902968 commit c98b441

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

cache/upload.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cache
22

33
import (
4-
"crypto/md5"
54
"encoding/json"
65
"fmt"
76
"time"
@@ -28,18 +27,7 @@ type MutUpload struct {
2827
}
2928

3029
func (p *MutUpload) Key() string {
31-
fingerprint := fmt.Sprintf(
32-
"%s:%s:%d:%d",
33-
p.Path,
34-
p.UpPath,
35-
p.Size,
36-
p.PartSize,
37-
)
38-
39-
return fmt.Sprintf(
40-
"mutupload-%x",
41-
md5.Sum([]byte(fingerprint)),
42-
)
30+
return fmt.Sprintf("mutupload-%s", p.UpPath)
4331
}
4432

4533
// 查询分片上传任务
@@ -53,7 +41,7 @@ func FindMutUpload(fn func(key string, entity *MutUpload) bool) ([]*MutUpload, e
5341
}
5442

5543
// 删除过期的分片上传记录
56-
if time.Since(item.CreateAt).Hours() > 12 {
44+
if time.Since(item.CreateAt).Hours() > 24 {
5745
FindMutUploadPart(func(key string, part *MutUploadPart) bool {
5846
if part.UploadID == item.UploadID {
5947
db.Delete([]byte(key), nil)
@@ -124,22 +112,15 @@ func AddMutUploadPart(entity *MutUploadPart) error {
124112
if err != nil {
125113
return err
126114
}
127-
128115
return db.Put([]byte(entity.Key()), data, nil)
129116
}
130117

131-
func DeleteByUploadID(uploadID string) error {
132-
FindMutUpload(func(key string, entity *MutUpload) bool {
133-
if entity.UploadID == uploadID {
134-
Delete(key)
135-
}
136-
return false
118+
func DeleteUpload(upPath, uploadID string) error {
119+
Range("mutupload-"+upPath, func(key, data []byte) {
120+
Delete(string(key))
137121
})
138-
FindMutUploadPart(func(key string, entity *MutUploadPart) bool {
139-
if entity.UploadID == uploadID {
140-
Delete(key)
141-
}
142-
return false
122+
Range("part-"+uploadID, func(key, data []byte) {
123+
Delete(string(key))
143124
})
144125
return nil
145126
}

session.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os.
571571
}
572572

573573
// 上传分片任务
574-
fmt.Println(skips)
575574
uploader := partial.NewMultiPartialUploader(
576575
DefaultBlockSize,
577576
fd,
@@ -614,7 +613,7 @@ func (sess *Session) putFileWithProgress(localPath, upPath string, localInfo os.
614613
return err
615614
}
616615
// 上传完成删除记录
617-
cache.DeleteByUploadID(initResult.UploadID)
616+
cache.DeleteUpload(upPath, initResult.UploadID)
618617
} else {
619618
cfg := &upyun.PutObjectConfig{
620619
Path: upPath,

0 commit comments

Comments
 (0)