From f53d4061106a13c3c682112c9116bf6ff0cda43a Mon Sep 17 00:00:00 2001 From: hoteas <925970985@qq.com> Date: Wed, 15 Jul 2026 23:02:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor(application):=20=E6=9B=B4=E6=96=B0=20C?= =?UTF-8?q?ache-Control=20=E9=80=BB=E8=BE=91=E4=B8=8E=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 简化 application.go 中的 Cache-Control 设置,固定为 public,移除 logLevel 对其的控制。 - 更新 var.go 中 logLevel 的说明,明确其不再影响静态资源的 Cache-Control。 - 修改 QuickStart 文档,反映 logLevel 的新行为与静态资源缓存策略。 --- application.go | 8 ++---- docs/QuickStart_快速上手.md | 2 +- static_cache_test.go | 53 +++++++++++++++++++++++++++++++++++++ var.go | 2 +- 4 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 static_cache_test.go diff --git a/application.go b/application.go index b15d1b1..3a51a3f 100644 --- a/application.go +++ b/application.go @@ -534,13 +534,9 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) { return } - //设置header + // 静态资源可缓存;logLevel 只控制日志/SQL,不再改 Cache-Control delete(header, "Content-Type") - if that.Config.GetCeilInt("logLevel") == 0 { - header.Set("Cache-Control", "public") - } else { - header.Set("Cache-Control", "no-cache") - } + header.Set("Cache-Control", "public") t := strings.LastIndex(path, ".") if t != -1 { diff --git a/docs/QuickStart_快速上手.md b/docs/QuickStart_快速上手.md index 3452002..bd6b4f1 100644 --- a/docs/QuickStart_快速上手.md +++ b/docs/QuickStart_快速上手.md @@ -80,7 +80,7 @@ func main() { | `modeRouterStrict` | false | 路由大小写敏感,false=忽略大小写 | | `crossDomain` | - | 跨域设置,空=不开启,auto=智能开启,或指定域名 | | `logFile` | - | 日志文件路径,如 `logs/20060102.txt` | -| `logLevel` | 0 | 日志等级,0=关闭,1=打印 | +| `logLevel` | 1 | 日志等级:0=仅错误,>=1=全部;同时控制 SQL 日志。**不**控制静态 `Cache-Control`(静态固定 `public`) | | `webConnectLogShow` | true | 是否显示访问日志 | | `defFile` | ["index.html"] | 目录默认访问文件 | diff --git a/static_cache_test.go b/static_cache_test.go new file mode 100644 index 0000000..0da0008 --- /dev/null +++ b/static_cache_test.go @@ -0,0 +1,53 @@ +package hotime + +import ( + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + . "code.hoteas.com/golang/hotime/cache" + . "code.hoteas.com/golang/hotime/common" + "code.hoteas.com/golang/hotime/log" +) + +// TestStaticFile_CacheControlPublic_WithLogLevel1 +// 静态文件在 logLevel>=1 时仍应 Cache-Control: public(与日志级别解耦)。 +func TestStaticFile_CacheControlPublic_WithLogLevel1(t *testing.T) { + dir := t.TempDir() + body := []byte("