From 4092863a9a1bf163910dbc3298dfc8e0c4fbff5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 9 Oct 2024 18:44:24 +0800 Subject: [PATCH 1/3] =?UTF-8?q?cache=E6=8E=A5=E5=8F=A3=E7=BA=A6=E6=9D=9F?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=9A=E4=B9=89=E7=BC=93=E5=AD=98=E5=89=A9?= =?UTF-8?q?=E4=BD=99=E6=97=B6=E9=97=B4=20+=20=E8=AF=B7=E6=B1=82=E9=A2=84?= =?UTF-8?q?=E7=83=AD=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/ICache.go | 12 ++++++++++++ define/cache.go | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 define/cache.go diff --git a/cache/ICache.go b/cache/ICache.go index 3d9c9be..3b1da7f 100644 --- a/cache/ICache.go +++ b/cache/ICache.go @@ -51,4 +51,16 @@ type ICache interface { // // Date : 16:46 2024/6/3 SetValue(cacheKey string, cacheValue string) error + // TTL 缓存剩余生命周期(单位: s) + // + // Author : go_developer@163.com<白茶清欢> + // + // Date : 18:39 2024/10/9 + TTL(cacheKey string) int64 + // PreHeatConfig 缓存预热配置 + // + // Author : go_developer@163.com<白茶清欢> + // + // Date : 18:42 2024/10/9 + PreHeatConfig() *define.CachePreHeatConfig } diff --git a/define/cache.go b/define/cache.go new file mode 100644 index 0000000..c503208 --- /dev/null +++ b/define/cache.go @@ -0,0 +1,19 @@ +// Package define ... +// +// Description : define ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-09 18:39 +package define + +// CachePreHeatConfig 缓存预热配置, MinPercent / MinTTL 同时配置, 则任意一个满足, 均进行预热 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:40 2024/10/9 +type CachePreHeatConfig struct { + Enable bool `json:"enable"` // 缓存预热是否可用 + MinPercent int `json:"min_percent"` // 最小百分比, 剩余有效期低于此百分比进行预热 + MinTTL int64 `json:"min_ttl"` // 最小剩余生命周期, 低于此百分比进行预热 +} -- 2.36.6 From 8ef7aea4feb4131cfa21c1b6029ab48ed1e48738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 9 Oct 2024 18:48:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=86=E7=A6=BB=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E4=B8=8E=E5=AF=B9=E5=90=8E=E7=AB=AF=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E7=9C=9F=E6=98=AF=E8=AF=B7=E6=B1=82=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/client.go b/client.go index 0ad8101..0cc7802 100644 --- a/client.go +++ b/client.go @@ -151,6 +151,16 @@ func (hc *HttpClient) Request() *define.Response { return cacheResult } + return hc.requestBackendApi() + +} + +// requestBackendApi 请求后端接口 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:47 2024/10/9 +func (hc *HttpClient) requestBackendApi() *define.Response { hc.Client.OnBeforeRequest(hc.getRequestValidateMiddleware()) // 请求参数验证中间件必注册 hc.Client.OnAfterResponse(hc.getResponseValidateMiddleware()) // 响应验证中间件必注册 var ( -- 2.36.6 From 10a21dc35745a9290e99faa65b17d28ef1db8de7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 9 Oct 2024 19:05:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?http=E8=AF=B7=E6=B1=82=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=A2=84=E7=83=AD=E6=8E=A5=E5=8F=A3=E6=95=B0=E6=8D=AE=E5=B9=B6?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 37 ++++++++++++++++++++++++++++++++++++- define/cache.go | 3 ++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 0cc7802..611271c 100644 --- a/client.go +++ b/client.go @@ -148,7 +148,42 @@ func (hc *HttpClient) Request() *define.Response { ) if cacheResult = hc.getCacheResult(); nil != cacheResult { - return cacheResult + // 判断是否开启预热 + inputCachePreHeatConfig := hc.cacheInstance.PreHeatConfig() + var cachePreHeatConfig define.CachePreHeatConfig + serialize.JSON.TransitionIgnoreError(inputCachePreHeatConfig, &cachePreHeatConfig) + if !cachePreHeatConfig.Enable || (cachePreHeatConfig.MinTTL <= 0 && cachePreHeatConfig.MinPercent <= 0 && !cachePreHeatConfig.Force) { + // 无预热配置或未启用预热或者未设置预热规则 + return cacheResult + } + go func() { + // 判断是否触发预热 + if cachePreHeatConfig.Force { + _ = hc.requestBackendApi() + return + } + // 将百分比的配置归一化成最小剩余时间的配置 + if cachePreHeatConfig.MinPercent > 0 { + expectMinTTL := hc.cacheInstance.CacheTime() * cachePreHeatConfig.MinPercent / 100 + if cachePreHeatConfig.MinTTL == 0 || cachePreHeatConfig.MinTTL > expectMinTTL { + cachePreHeatConfig.MinTTL = expectMinTTL + } + } + if cachePreHeatConfig.MinTTL <= 0 { + // 未配置最小剩余时间 + return + } + ttl := hc.cacheInstance.TTL(cacheResult.CacheInfo.CacheKey) + if ttl < 0 { + // 不存在或者未设置有效期 + return + } + + if ttl <= cachePreHeatConfig.MinTTL { + // 配置了最小剩余时间,并且key剩余有效期小于最小剩余时间 + _ = hc.requestBackendApi() + } + }() } return hc.requestBackendApi() diff --git a/define/cache.go b/define/cache.go index c503208..24c74a3 100644 --- a/define/cache.go +++ b/define/cache.go @@ -14,6 +14,7 @@ package define // Date : 18:40 2024/10/9 type CachePreHeatConfig struct { Enable bool `json:"enable"` // 缓存预热是否可用 - MinPercent int `json:"min_percent"` // 最小百分比, 剩余有效期低于此百分比进行预热 + MinPercent int64 `json:"min_percent"` // 最小百分比, 剩余有效期低于此百分比进行预热 MinTTL int64 `json:"min_ttl"` // 最小剩余生命周期, 低于此百分比进行预热 + Force bool `json:"force"` // 启用预热的情况下, 强制预热, 会忽略 MinPercent / MinTTL 的配置 } -- 2.36.6