package metacache import ( "context" "time" "gitlink.org.cn/cloudream/common/pkgs/logger" stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals" corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator" jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types" ) func (m *MetaCacheHost) AddHubMeta() *HubMeta { meta := &HubMeta{} meta.cache = NewSimpleMetaCache(SimpleMetaCacheConfig[jcstypes.HubID, jcstypes.Hub]{ Getter: meta.load, Expire: time.Minute * 5, }) m.caches = append(m.caches, meta) return meta } type HubMeta struct { cache *SimpleMetaCache[jcstypes.HubID, jcstypes.Hub] } func (h *HubMeta) Get(hubID jcstypes.HubID) *jcstypes.Hub { v, ok := h.cache.Get(hubID) if ok { return &v } return nil } func (h *HubMeta) GetMany(hubIDs []jcstypes.HubID) []*jcstypes.Hub { vs, oks := h.cache.GetMany(hubIDs) ret := make([]*jcstypes.Hub, len(vs)) for i := range vs { if oks[i] { ret[i] = &vs[i] } } return ret } func (h *HubMeta) ClearOutdated() { h.cache.ClearOutdated() } func (h *HubMeta) load(keys []jcstypes.HubID) ([]jcstypes.Hub, []bool) { vs := make([]jcstypes.Hub, len(keys)) oks := make([]bool, len(keys)) coorCli := stgglb.CoordinatorRPCPool.Get() defer coorCli.Release() get, cerr := coorCli.GetHubs(context.Background(), corrpc.NewGetHubs(keys)) if cerr != nil { logger.Warnf("get hubs: %v", cerr) return vs, oks } for i := range keys { if get.Hubs[i] != nil { vs[i] = *get.Hubs[i] oks[i] = true } } return vs, oks }