package cache import ( "basic.com/pubsub/cache.git/shardmap" "basic.com/pubsub/protomsg.git" "basic.com/valib/bhomeclient.git" "basic.com/valib/bhomedbapi.git" "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" "basic.com/valib/logger.git" "encoding/json" "errors" ) var cMap *shardmap.ShardMap const ( SERVER_KEY = "SERVERINFO" //服务器信息 SCENERULES = "SCENERULES" //摄像机场景列表 ) func UpdateBySub(msg *bhome_msg.MsgPublish) { switch string(msg.Topic) { case bhomeclient.Proc_System_Service: initServerInfo() case bhomeclient.Proc_Scene_Service: var protoMsg protomsg.DbChangeMessage if err := json.Unmarshal(msg.Data, &protoMsg); err == nil { initCameraRules() } } } //缓存一些其他数据库的数据,并通过订阅消息实时更新 func InitDataCache() { cMap = shardmap.New(uint8(32)) initServerInfo() //初始化服务器配置信息 initCameraRules() } func initServerInfo() { var api bhomedbapi.SysSetApi b, s := api.GetServerInfo() logger.Debug("GetServerInfo b:", b, "s:", s) if b && len(s.ServerId) != 0 { cMap.Set(SERVER_KEY, s) } } func GetServerInfo() (conf protomsg.LocalConfig, err error) { config, b := cMap.Get(SERVER_KEY) if b { return config.(protomsg.LocalConfig), nil } else { initServerInfo() nc, nb := cMap.Get(SERVER_KEY) if nb { return nc.(protomsg.LocalConfig), nil } else { return conf, errors.New("conf not found") } } } func initCameraRules() { var api bhomedbapi.CameraRuleApi b, allCamRules := api.FindAll() logger.Debug("initCameraRules b:", b, " len(allCamRules):", len(allCamRules)) if b { cMap.Set(SCENERULES, allCamRules) } } func GetCameraRules() ([]protomsg.CameraAndRules, error) { allRules, b := cMap.Get(SCENERULES) if b { return allRules.([]protomsg.CameraAndRules), nil } else { initCameraRules() nr, nb := cMap.Get(SCENERULES) if nb { return nr.([]protomsg.CameraAndRules), nil } else { return nil, errors.New("no CameraAndRules cached") } } }