diff --git a/internal/cache/file_cache.go b/internal/cache/file_cache.go index 1494b5f..2e954eb 100644 --- a/internal/cache/file_cache.go +++ b/internal/cache/file_cache.go @@ -20,11 +20,12 @@ package cache import ( "container/list" "slices" - "strconv" "strings" "sync" "sync/atomic" "time" + + "rua.plus/lolly/internal/utils" ) // FileEntry 文件缓存条目,存储单个文件的缓存信息。 @@ -43,14 +44,7 @@ type FileEntry struct { // generateETag 基于 ModTime 和 Size 生成 ETag。 // 使用 strconv.AppendInt 避免 fmt.Sprintf 分配。 func generateETag(modTime time.Time, size int64) string { - var buf [32]byte - b := buf[:0] - b = append(b, '"') - b = strconv.AppendInt(b, modTime.Unix(), 16) - b = append(b, '-') - b = strconv.AppendInt(b, size, 16) - b = append(b, '"') - return string(b) + return utils.GenerateETag(modTime, size) } // FileCache 文件缓存,支持 LRU 淘汰策略。 diff --git a/internal/handler/static.go b/internal/handler/static.go index 2b5ce90..e8810f0 100644 --- a/internal/handler/static.go +++ b/internal/handler/static.go @@ -22,7 +22,6 @@ import ( "fmt" "os" "path/filepath" - "strconv" "strings" "time" @@ -913,14 +912,7 @@ func (h *StaticHandler) validateSymlink(filePath string) error { // generateETag 基于 ModTime 和 Size 生成 ETag。 // 使用 strconv.AppendInt 避免 fmt.Sprintf 分配。 func generateETag(modTime time.Time, size int64) string { - var buf [32]byte - b := buf[:0] - b = append(b, '"') - b = strconv.AppendInt(b, modTime.Unix(), 16) - b = append(b, '-') - b = strconv.AppendInt(b, size, 16) - b = append(b, '"') - return string(b) + return utils.GenerateETag(modTime, size) } // isNotModified 检查条件请求是否匹配(返回 true 表示应返回 304)。 diff --git a/internal/utils/etag.go b/internal/utils/etag.go new file mode 100644 index 0000000..3385b15 --- /dev/null +++ b/internal/utils/etag.go @@ -0,0 +1,20 @@ +package utils + +import ( + "strconv" + "time" +) + +// GenerateETag 基于 ModTime 和 Size 生成 ETag。 +// 使用 strconv.AppendInt 避免 fmt.Sprintf 分配。 +// 格式: "-" +func GenerateETag(modTime time.Time, size int64) string { + var buf [32]byte + b := buf[:0] + b = append(b, '"') + b = strconv.AppendInt(b, modTime.Unix(), 16) + b = append(b, '-') + b = strconv.AppendInt(b, size, 16) + b = append(b, '"') + return string(b) +}