From 3b2360162cfa774c4724246aefcbc606586dc6c6 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 8 May 2026 18:06:13 +0800 Subject: [PATCH] refactor(utils): add unified ETag generation function Extract duplicate generateETag function from handler/static.go and cache/file_cache.go into internal/utils/etag.go. Both functions were identical, using strconv.AppendInt for zero-allocation ETag generation. - Create utils.GenerateETag(modTime, size) as the unified implementation - Update handler/static.go to call utils.GenerateETag - Update cache/file_cache.go to call utils.GenerateETag - Remove unused strconv import from static.go Co-Authored-By: Claude Opus 4.7 --- internal/cache/file_cache.go | 12 +++--------- internal/handler/static.go | 10 +--------- internal/utils/etag.go | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 internal/utils/etag.go 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) +}