From eabceecaaea04ec154ed611eb9e3814e4d0f77d5 Mon Sep 17 00:00:00 2001 From: Victor_Jay Date: Sun, 31 May 2026 02:06:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=9D=99=E6=80=81=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E5=93=88=E5=B8=8C=E7=AE=97=E6=B3=95=E4=BB=8E=20SHA-1?= =?UTF-8?q?=20=E6=94=B9=E4=B8=BA=20CRC32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CRC32 更快且语义更匹配缓存破坏场景 - 输出自然为 8 位 hex,无需截取 --- internal/common/helper.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/common/helper.go b/internal/common/helper.go index fd85e94..35d55ee 100644 --- a/internal/common/helper.go +++ b/internal/common/helper.go @@ -1,9 +1,8 @@ package common import ( - "crypto/sha1" - "encoding/hex" "fmt" + "hash/crc32" "io/fs" "log" "net/http" @@ -11,6 +10,7 @@ import ( "os" "path/filepath" "regexp" + "strconv" "sync" "metazone.cc/metalab/internal/model" @@ -24,7 +24,7 @@ var ( assetOnce sync.Once ) -// ComputeAssetHashes 遍历 static 目录,计算每个文件的 SHA-1 前 8 位作为版本号 +// ComputeAssetHashes 遍历 static 目录,计算每个文件的 CRC32作为版本号 func ComputeAssetHashes(templateDir string) { assetHashes = make(map[string]string) staticDir := filepath.Join(templateDir, "static") @@ -41,8 +41,8 @@ func ComputeAssetHashes(templateDir string) { if err != nil { return nil } - h := sha1.Sum(data) - shortHash := hex.EncodeToString(h[:])[:8] + h := crc32.ChecksumIEEE(data) + shortHash := strconv.FormatUint(uint64(h), 16) // 生成 URL 路径:/static/... rel, _ := filepath.Rel(filepath.Dir(templateDir), path) urlPath := "/" + filepath.ToSlash(rel) @@ -63,8 +63,8 @@ func ComputeAssetHashes(templateDir string) { if err != nil { return nil } - h := sha1.Sum(data) - shortHash := hex.EncodeToString(h[:])[:8] + h := crc32.ChecksumIEEE(data) + shortHash := strconv.FormatUint(uint64(h), 16) rel, _ := filepath.Rel(filepath.Dir(templateDir), path) urlPath := "/" + filepath.ToSlash(rel) assetHashes[urlPath] = shortHash @@ -84,8 +84,8 @@ func ComputeAssetHashes(templateDir string) { if err != nil { return nil } - h := sha1.Sum(data) - shortHash := hex.EncodeToString(h[:])[:8] + h := crc32.ChecksumIEEE(data) + shortHash := strconv.FormatUint(uint64(h), 16) rel, _ := filepath.Rel(filepath.Dir(templateDir), path) urlPath := "/" + filepath.ToSlash(rel) assetHashes[urlPath] = shortHash