From c4dcfb1cd64ae39a1dc1e937b3e992cf420a09b6 Mon Sep 17 00:00:00 2001 From: XiMo-210 <2831802697@qq.com> Date: Sun, 17 Nov 2024 05:54:56 +0800 Subject: [PATCH] =?UTF-8?q?perf(yxy):=20=E4=BD=BF=E7=94=A8singleflight?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=B5=E8=B4=B9AuthToken=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E9=98=B2=E6=AD=A2=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=87=BB=E7=A9=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/yxyServices/cacheService.go | 26 +++++++++++++++++------- go.mod | 1 + go.sum | 1 + 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/services/yxyServices/cacheService.go b/app/services/yxyServices/cacheService.go index b95c24e..7015875 100644 --- a/app/services/yxyServices/cacheService.go +++ b/app/services/yxyServices/cacheService.go @@ -5,10 +5,15 @@ import ( "time" r "wejh-go/config/redis" + "golang.org/x/sync/singleflight" + "github.com/go-redis/redis/v8" ) -var ctx = context.Background() +var ( + ctx = context.Background() + g singleflight.Group +) func GetElecRoomStrConcat(token, campus, yxyUid string) (*string, error) { cacheKey := "elec:room_str_concat:" + campus + ":" + yxyUid @@ -33,15 +38,22 @@ func GetElecAuthToken(yxyUid string) (*string, error) { cacheKey := "elec:auth_token:" + yxyUid cachedToken, err := r.RedisClient.Get(ctx, cacheKey).Result() if err == redis.Nil { - token, err := Auth(yxyUid) - if err != nil { - return nil, err - } - err = r.RedisClient.Set(ctx, cacheKey, *token, 7*24*time.Hour).Err() + // 使用 singleflight 防止缓存击穿 + token, err, _ := g.Do(cacheKey, func() (interface{}, error) { + t, e := Auth(yxyUid) + if e != nil { + return nil, e + } + e = r.RedisClient.Set(ctx, cacheKey, *t, 7*24*time.Hour).Err() + if e != nil { + return nil, e + } + return t, nil + }) if err != nil { return nil, err } - return token, nil + return token.(*string), nil } else if err != nil { return nil, err } diff --git a/go.mod b/go.mod index 5b87874..adae961 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/ugorji/go v1.2.5 // indirect github.com/xuri/excelize/v2 v2.8.0 go.opentelemetry.io/otel v0.20.0 // indirect + golang.org/x/sync v0.1.0 gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gorm.io/driver/mysql v1.0.6 diff --git a/go.sum b/go.sum index d00d385..beb386b 100644 --- a/go.sum +++ b/go.sum @@ -406,6 +406,7 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=