Compare commits

...

3 Commits

  1. 18
      configHelper.go
  2. 16
      redisHelper.go

18
configHelper.go

@ -4,6 +4,7 @@ import (
"context"
"gitea.anxinyun.cn/container/common_models/constant/settlementParam"
"strings"
"sync"
//"encoding/json"
"errors"
@ -31,18 +32,17 @@ var StructureCache map[int]common_models.Structure
// var stationThreshold map[int]common_models.Threshold
// var aggThreshold map[string]common_models.AggThreshold
var taskTime *time.Ticker
// var taskTime *time.Ticker
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 {

16
redisHelper.go

@ -31,7 +31,23 @@ func NewRedisHelper(master string, address ...string) *RedisHelper {
}
func (the *RedisHelper) InitialCluster(master string, address ...string) {
var opts *redis.UniversalOptions
if master != "" {
opts = &redis.UniversalOptions{Addrs: address, MasterName: "mymaster", PoolSize: 10}
} else {
opts = &redis.UniversalOptions{Addrs: address, PoolSize: 10}
}
the.rdb = redis.NewUniversalClient(opts)
if the.rdb == nil {
log.Fatal("Failed to initialize Redis client")
}
log.Printf("Redis client initialized with addresses: %s", address)
the.isReady = true
}
func (the *RedisHelper) InitialCluster_old(master string, address ...string) {
if master != "" {
the.rdb = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: address,

Loading…
Cancel
Save