perf: 优化上传文件缓存策略
- 上传新文件时删除同类型旧文件,避免文件堆积 - 文件名带时间戳,每次上传生成新 URL - 缓存时间设为 1 年 + immutable,文件更新后 URL 变化自动重新请求
This commit is contained in:
+1
-1
@@ -233,7 +233,7 @@ func startServer() {
|
||||
|
||||
r.Use(func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/uploads/") {
|
||||
c.Header("Cache-Control", "public, max-age=3600")
|
||||
c.Header("Cache-Control", "public, max-age=31536000, immutable")
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
|
||||
@@ -382,6 +382,12 @@ func handleUploadSystemImage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
pattern := filepath.Join(uploadDir, uploadType+"_*")
|
||||
oldFiles, _ := filepath.Glob(pattern)
|
||||
for _, oldFile := range oldFiles {
|
||||
os.Remove(oldFile)
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("%s_%d%s", uploadType, time.Now().UnixNano(), ext)
|
||||
filePath := filepath.Join(uploadDir, filename)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user