You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

hubmeta.go 1.5 kB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package metacache
  2. import (
  3. "context"
  4. "time"
  5. "gitlink.org.cn/cloudream/common/pkgs/logger"
  6. stgglb "gitlink.org.cn/cloudream/jcs-pub/common/globals"
  7. corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
  8. cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
  9. )
  10. func (m *MetaCacheHost) AddHubMeta() *HubMeta {
  11. meta := &HubMeta{}
  12. meta.cache = NewSimpleMetaCache(SimpleMetaCacheConfig[cortypes.HubID, cortypes.Hub]{
  13. Getter: meta.load,
  14. Expire: time.Minute * 5,
  15. })
  16. m.caches = append(m.caches, meta)
  17. return meta
  18. }
  19. type HubMeta struct {
  20. cache *SimpleMetaCache[cortypes.HubID, cortypes.Hub]
  21. }
  22. func (h *HubMeta) Get(hubID cortypes.HubID) *cortypes.Hub {
  23. v, ok := h.cache.Get(hubID)
  24. if ok {
  25. return &v
  26. }
  27. return nil
  28. }
  29. func (h *HubMeta) GetMany(hubIDs []cortypes.HubID) []*cortypes.Hub {
  30. vs, oks := h.cache.GetMany(hubIDs)
  31. ret := make([]*cortypes.Hub, len(vs))
  32. for i := range vs {
  33. if oks[i] {
  34. ret[i] = &vs[i]
  35. }
  36. }
  37. return ret
  38. }
  39. func (h *HubMeta) ClearOutdated() {
  40. h.cache.ClearOutdated()
  41. }
  42. func (h *HubMeta) load(keys []cortypes.HubID) ([]cortypes.Hub, []bool) {
  43. vs := make([]cortypes.Hub, len(keys))
  44. oks := make([]bool, len(keys))
  45. coorCli := stgglb.CoordinatorRPCPool.Get()
  46. defer coorCli.Release()
  47. get, cerr := coorCli.GetHubs(context.Background(), corrpc.NewGetHubs(keys))
  48. if cerr != nil {
  49. logger.Warnf("get hubs: %v", cerr)
  50. return vs, oks
  51. }
  52. for i := range keys {
  53. if get.Hubs[i] != nil {
  54. vs[i] = *get.Hubs[i]
  55. oks[i] = true
  56. }
  57. }
  58. return vs, oks
  59. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。