refactor: remove redundant generateETag wrappers, use utils.GenerateETag directly
This commit is contained in:
parent
ae3c167cd6
commit
66f608f25b
8
internal/cache/file_cache.go
vendored
8
internal/cache/file_cache.go
vendored
@ -42,12 +42,6 @@ type FileEntry struct {
|
|||||||
ContentType string // 预计算的 MIME 类型,避免每次请求重新检测
|
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 淘汰策略。
|
// FileCache 文件缓存,支持 LRU 淘汰策略。
|
||||||
//
|
//
|
||||||
// 该结构体实现了基于内存的文件缓存,支持按条目数和内存大小限制进行淘汰。
|
// 该结构体实现了基于内存的文件缓存,支持按条目数和内存大小限制进行淘汰。
|
||||||
@ -178,7 +172,7 @@ func (c *FileCache) Set(path string, data []byte, size int64, modTime time.Time,
|
|||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
// 预计算 ETag
|
// 预计算 ETag
|
||||||
etag := generateETag(modTime, size)
|
etag := utils.GenerateETag(modTime, size)
|
||||||
|
|
||||||
// 检查是否已存在
|
// 检查是否已存在
|
||||||
if entry, ok := c.entries[path]; ok {
|
if entry, ok := c.entries[path]; ok {
|
||||||
|
|||||||
@ -625,7 +625,7 @@ func (h *StaticHandler) handleStandard(ctx *fasthttp.RequestCtx, reqPath string)
|
|||||||
// - info: 文件信息(用于判断文件大小和修改时间)
|
// - info: 文件信息(用于判断文件大小和修改时间)
|
||||||
func (h *StaticHandler) serveFile(ctx *fasthttp.RequestCtx, filePath string, info os.FileInfo, skipCacheLookup bool) {
|
func (h *StaticHandler) serveFile(ctx *fasthttp.RequestCtx, filePath string, info os.FileInfo, skipCacheLookup bool) {
|
||||||
// 生成 ETag 并检查条件请求(在预压缩检查之前)
|
// 生成 ETag 并检查条件请求(在预压缩检查之前)
|
||||||
etag := generateETag(info.ModTime(), info.Size())
|
etag := utils.GenerateETag(info.ModTime(), info.Size())
|
||||||
if isNotModified(ctx, etag, info.ModTime()) {
|
if isNotModified(ctx, etag, info.ModTime()) {
|
||||||
ctx.Response.SetStatusCode(fasthttp.StatusNotModified)
|
ctx.Response.SetStatusCode(fasthttp.StatusNotModified)
|
||||||
ctx.Response.Header.Set("ETag", etag)
|
ctx.Response.Header.Set("ETag", etag)
|
||||||
@ -829,12 +829,6 @@ func (h *StaticHandler) validateSymlink(filePath string) error {
|
|||||||
return nil
|
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)。
|
// isNotModified 检查条件请求是否匹配(返回 true 表示应返回 304)。
|
||||||
func isNotModified(ctx *fasthttp.RequestCtx, etag string, modTime time.Time) bool {
|
func isNotModified(ctx *fasthttp.RequestCtx, etag string, modTime time.Time) bool {
|
||||||
if match := ctx.Request.Header.Peek("If-None-Match"); len(match) > 0 {
|
if match := ctx.Request.Header.Peek("If-None-Match"); len(match) > 0 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user