From 66f608f25ba9784f1aa611c2e3892224312a5a19 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 3 Jun 2026 17:51:50 +0800 Subject: [PATCH] refactor: remove redundant generateETag wrappers, use utils.GenerateETag directly --- internal/cache/file_cache.go | 8 +------- internal/handler/static.go | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/internal/cache/file_cache.go b/internal/cache/file_cache.go index 55e75c2..a65d30a 100644 --- a/internal/cache/file_cache.go +++ b/internal/cache/file_cache.go @@ -42,12 +42,6 @@ type FileEntry struct { ContentType string // 预计算的 MIME 类型,避免每次请求重新检测 } -// generateETag 基于 ModTime 和 Size 生成 ETag。 -// 使用 strconv.AppendInt 避免 fmt.Sprintf 分配。 -func generateETag(modTime time.Time, size int64) string { - return utils.GenerateETag(modTime, size) -} - // FileCache 文件缓存,支持 LRU 淘汰策略。 // // 该结构体实现了基于内存的文件缓存,支持按条目数和内存大小限制进行淘汰。 @@ -178,7 +172,7 @@ func (c *FileCache) Set(path string, data []byte, size int64, modTime time.Time, defer c.mu.Unlock() // 预计算 ETag - etag := generateETag(modTime, size) + etag := utils.GenerateETag(modTime, size) // 检查是否已存在 if entry, ok := c.entries[path]; ok { diff --git a/internal/handler/static.go b/internal/handler/static.go index 07d1801..529bb3d 100644 --- a/internal/handler/static.go +++ b/internal/handler/static.go @@ -625,7 +625,7 @@ func (h *StaticHandler) handleStandard(ctx *fasthttp.RequestCtx, reqPath string) // - info: 文件信息(用于判断文件大小和修改时间) func (h *StaticHandler) serveFile(ctx *fasthttp.RequestCtx, filePath string, info os.FileInfo, skipCacheLookup bool) { // 生成 ETag 并检查条件请求(在预压缩检查之前) - etag := generateETag(info.ModTime(), info.Size()) + etag := utils.GenerateETag(info.ModTime(), info.Size()) if isNotModified(ctx, etag, info.ModTime()) { ctx.Response.SetStatusCode(fasthttp.StatusNotModified) ctx.Response.Header.Set("ETag", etag) @@ -829,12 +829,6 @@ func (h *StaticHandler) validateSymlink(filePath string) error { return nil } -// generateETag 基于 ModTime 和 Size 生成 ETag。 -// 使用 strconv.AppendInt 避免 fmt.Sprintf 分配。 -func generateETag(modTime time.Time, size int64) string { - return utils.GenerateETag(modTime, size) -} - // isNotModified 检查条件请求是否匹配(返回 true 表示应返回 304)。 func isNotModified(ctx *fasthttp.RequestCtx, etag string, modTime time.Time) bool { if match := ctx.Request.Header.Peek("If-None-Match"); len(match) > 0 {