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

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