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.

user_space_meta.go 2.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  9. )
  10. func (m *MetaCacheHost) AddStorageMeta() *UserSpaceMeta {
  11. meta := &UserSpaceMeta{
  12. host: m,
  13. }
  14. meta.cache = NewSimpleMetaCache(SimpleMetaCacheConfig[jcstypes.UserSpaceID, jcstypes.UserSpaceDetail]{
  15. Getter: meta.load,
  16. Expire: time.Minute * 5,
  17. })
  18. m.caches = append(m.caches, meta)
  19. return meta
  20. }
  21. type UserSpaceMeta struct {
  22. host *MetaCacheHost
  23. cache *SimpleMetaCache[jcstypes.UserSpaceID, jcstypes.UserSpaceDetail]
  24. }
  25. func (s *UserSpaceMeta) Get(spaceID jcstypes.UserSpaceID) *jcstypes.UserSpaceDetail {
  26. v, ok := s.cache.Get(spaceID)
  27. if ok {
  28. return &v
  29. }
  30. return nil
  31. }
  32. func (s *UserSpaceMeta) GetMany(spaceIDs []jcstypes.UserSpaceID) []*jcstypes.UserSpaceDetail {
  33. vs, oks := s.cache.GetMany(spaceIDs)
  34. ret := make([]*jcstypes.UserSpaceDetail, len(vs))
  35. for i := range vs {
  36. if oks[i] {
  37. ret[i] = &vs[i]
  38. }
  39. }
  40. return ret
  41. }
  42. func (s *UserSpaceMeta) ClearOutdated() {
  43. s.cache.ClearOutdated()
  44. }
  45. func (s *UserSpaceMeta) Drop(keys []jcstypes.UserSpaceID) {
  46. s.cache.Drop(keys)
  47. }
  48. func (s *UserSpaceMeta) load(keys []jcstypes.UserSpaceID) ([]jcstypes.UserSpaceDetail, []bool) {
  49. vs := make([]jcstypes.UserSpaceDetail, len(keys))
  50. oks := make([]bool, len(keys))
  51. spaces, err := s.host.db.UserSpace().BatchGetByID(s.host.db.DefCtx(), keys)
  52. if err != nil {
  53. logger.Warnf("batch get user space by id: %v", err)
  54. return vs, oks
  55. }
  56. detailMap := make(map[jcstypes.UserSpaceID]*jcstypes.UserSpaceDetail)
  57. for i := range spaces {
  58. detailMap[spaces[i].UserSpaceID] = &jcstypes.UserSpaceDetail{
  59. UserID: stgglb.UserID,
  60. UserSpace: spaces[i],
  61. }
  62. }
  63. if !stgglb.StandaloneMode {
  64. coorCli := stgglb.CoordinatorRPCPool.Get()
  65. defer coorCli.Release()
  66. stgs := make([]jcstypes.StorageType, len(spaces))
  67. for i := range spaces {
  68. stgs[i] = spaces[i].Storage
  69. }
  70. selectHubs, cerr := coorCli.SelectStorageHub(context.Background(), &corrpc.SelectStorageHub{
  71. Storages: stgs,
  72. })
  73. if cerr != nil {
  74. logger.Warnf("get storage details: %v", cerr)
  75. return vs, oks
  76. }
  77. for i := range spaces {
  78. detailMap[spaces[i].UserSpaceID].RecommendHub = selectHubs.Hubs[i]
  79. }
  80. }
  81. for i := range keys {
  82. if detail, ok := detailMap[keys[i]]; ok {
  83. vs[i] = *detail
  84. oks[i] = true
  85. }
  86. }
  87. return vs, oks
  88. }

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