From f17b2403498dd28387887c94862e168e6f6c6a93 Mon Sep 17 00:00:00 2001 From: yfh Date: Tue, 12 Nov 2024 14:40:05 +0800 Subject: [PATCH] =?UTF-8?q?(*)=E8=A7=A3=E5=86=B3DeviceInfoCache=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E5=AE=89=E5=85=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configHelper.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/configHelper.go b/configHelper.go index a736b7e..213b920 100644 --- a/configHelper.go +++ b/configHelper.go @@ -4,6 +4,7 @@ import ( "context" "gitea.anxinyun.cn/container/common_models/constant/settlementParam" "strings" + "sync" //"encoding/json" "errors" @@ -37,12 +38,11 @@ type ConfigHelper struct { redisHelper *RedisHelper //普通缓存用 chainedCache *ChainedCache ctx context.Context + mu sync.RWMutex } func NewConfigHelper(redisAddr string) *ConfigHelper { - initDeviceInfoMapCache() - return &ConfigHelper{ redisHelper: NewRedisHelper("", redisAddr), chainedCache: NewChainedCache(redisAddr), @@ -94,8 +94,10 @@ func initDeviceInfoMapCache() { } } -// GetDeviceInfo 通过 func (the *ConfigHelper) GetDeviceInfo(deviceId string) (*common_models.DeviceInfo, error) { + the.mu.Lock() + defer the.mu.Unlock() + deviceInfo, ok := DeviceInfoCache[deviceId] if !ok { //去redis查询 device, err := the.GetIotaDevice(deviceId) @@ -130,8 +132,8 @@ func (the *ConfigHelper) GetDeviceInfo(deviceId string) (*common_models.DeviceIn DeviceInfoCache[deviceId] = deviceInfo } return &deviceInfo, nil - } + func (the *ConfigHelper) GetFormulaInfo(formulaId int) (common_models.Formula, error) { var err error result, ok := FormulaCache[formulaId] @@ -407,7 +409,7 @@ func (the *ConfigHelper) GetStationGroup(groupId int) (common_models.StationGrou } err = json.Unmarshal([]byte(v), &group) if err != nil { - log.Printf("json unmarshal error:%s \n", err.Error()) + log.Printf("【redisKey.Group】【%s】json unmarshal error:%s \n", k, err.Error()) } } @@ -429,7 +431,7 @@ func (the *ConfigHelper) GetStationGroupInfo(stationId int) (common_models.Stati if v, ok := value.(string); ok { err = json.Unmarshal([]byte(v), &info) if err != nil { - log.Printf("json unmarshal error:%s \n", err.Error()) + log.Printf("【redisKey.Station_group】【%s】json unmarshal error:%s \n", k, err.Error()) } } return info, err @@ -448,7 +450,7 @@ func (the *ConfigHelper) GetStationCorrGroups(stationId int) ([]common_models.St if v, ok := value.(string); ok { err = json.Unmarshal([]byte(v), &groupIds) if err != nil { - log.Printf("json unmarshal error:%s \n", err.Error()) + log.Printf("【redisKey.Station_corr_group】【%s】json unmarshal error:%s \n", k, err.Error()) } } if err != nil {