From 0cf943fedebeedfdbe2ab4f444203db531967612 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 24 Apr 2026 10:41:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor(config,test):=20=E4=BC=98=E5=8C=96=20p?= =?UTF-8?q?arseSize=20=E4=B8=BA=20switch=20=E5=B9=B6=E9=80=82=E9=85=8D=20N?= =?UTF-8?q?ewProxyCache=20=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 parseSize 的 if-else 改为 switch 语句;更新集成测试中 NewProxyCache 调用以匹配新增的 stale_if_error/stale_if_timeout 参数。 Co-Authored-By: Claude Opus 4.7 --- internal/config/config.go | 5 +++-- internal/integration/cache_integration_test.go | 18 +++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 10977ff..93ee89b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -458,10 +458,11 @@ func parseSize(s string) (int, error) { multiplier := 1 numStr := s - if unit == "k" { + switch unit { + case "k": multiplier = 1024 numStr = s[:len(s)-1] - } else if unit == "m" { + case "m": multiplier = 1024 * 1024 numStr = s[:len(s)-1] } diff --git a/internal/integration/cache_integration_test.go b/internal/integration/cache_integration_test.go index 794b9d6..bc7c8fb 100644 --- a/internal/integration/cache_integration_test.go +++ b/internal/integration/cache_integration_test.go @@ -27,7 +27,7 @@ func TestProxyCacheCreation(t *testing.T) { }, } - pc := cache.NewProxyCache(rules, true, 30*time.Second) + pc := cache.NewProxyCache(rules, true, 30*time.Second, 0, 0) require.NotNil(t, pc) } @@ -35,7 +35,7 @@ func TestProxyCacheCreation(t *testing.T) { func TestProxyCacheDisabled(t *testing.T) { rules := []cache.ProxyCacheRule{} - pc := cache.NewProxyCache(rules, false, 0) + pc := cache.NewProxyCache(rules, false, 0, 0, 0) require.NotNil(t, pc) } @@ -50,7 +50,7 @@ func TestProxyCacheSetAndGet(t *testing.T) { }, } - pc := cache.NewProxyCache(rules, true, 30*time.Second) + pc := cache.NewProxyCache(rules, true, 30*time.Second, 0, 0) require.NotNil(t, pc) origKey := "GET:/test" @@ -85,7 +85,7 @@ func TestProxyCacheMiss(t *testing.T) { }, } - pc := cache.NewProxyCache(rules, true, 30*time.Second) + pc := cache.NewProxyCache(rules, true, 30*time.Second, 0, 0) require.NotNil(t, pc) origKey := "GET:/nonexistent" @@ -109,7 +109,7 @@ func TestProxyCacheExpiration(t *testing.T) { }, } - pc := cache.NewProxyCache(rules, true, 50*time.Millisecond) + pc := cache.NewProxyCache(rules, true, 50*time.Millisecond, 0, 0) require.NotNil(t, pc) origKey := "GET:/expiring" @@ -146,7 +146,7 @@ func TestProxyCacheStale(t *testing.T) { } // staleTime = 200ms,允许过期后 200ms 内复用 - pc := cache.NewProxyCache(rules, true, 200*time.Millisecond) + pc := cache.NewProxyCache(rules, true, 200*time.Millisecond, 0, 0) require.NotNil(t, pc) origKey := "GET:/stale" @@ -178,7 +178,7 @@ func TestProxyCacheHashKeyCollision(t *testing.T) { }, } - pc := cache.NewProxyCache(rules, true, 30*time.Second) + pc := cache.NewProxyCache(rules, true, 30*time.Second, 0, 0) require.NotNil(t, pc) // 两个不同的 key @@ -213,7 +213,7 @@ func TestProxyCacheConcurrent(t *testing.T) { }, } - pc := cache.NewProxyCache(rules, true, 30*time.Second) + pc := cache.NewProxyCache(rules, true, 30*time.Second, 0, 0) require.NotNil(t, pc) // 并发写入 @@ -251,7 +251,7 @@ func TestProxyCacheUsesCount(t *testing.T) { }, } - pc := cache.NewProxyCache(rules, true, 30*time.Second) + pc := cache.NewProxyCache(rules, true, 30*time.Second, 0, 0) require.NotNil(t, pc) origKey := "GET:/counted"