Browse Source

(*)解决DeviceInfoCache访问安全问题

dev
yfh 2 months ago
parent
commit
f17b240349
  1. 16
      configHelper.go

16
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 {

Loading…
Cancel
Save