infrastructure.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package infrastructure
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache"
  4. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache/local"
  5. redisCache "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache/redis"
  6. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  7. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/data_service"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/operations"
  9. messageQueueCommon "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/common"
  10. redisMessageQueue "git.sxidc.com/go-framework/baize/framework/core/infrastructure/message_queue/redis"
  11. "git.sxidc.com/go-tools/utils/strutils"
  12. )
  13. type Config struct {
  14. DatabaseConfig
  15. CacheConfig
  16. MessageQueueConfig
  17. }
  18. type DatabaseConfig struct {
  19. Operations *operations.Config `json:"operations" yaml:"operations"`
  20. DataService *data_service.Config `json:"data_service" yaml:"data_service"`
  21. }
  22. type CacheConfig struct {
  23. Namespace string `json:"namespace" yaml:"namespace"`
  24. Redis *RedisConfig `json:"redis" yaml:"redis"`
  25. }
  26. type MessageQueueConfig struct {
  27. Redis *RedisConfig `json:"redis" yaml:"redis"`
  28. MaxLen int64
  29. }
  30. type RedisConfig struct {
  31. Address string `json:"address" yaml:"address"`
  32. UserName string `json:"user_name" yaml:"user_name"`
  33. Password string `json:"password" yaml:"password"`
  34. DB int `json:"db" yaml:"db"`
  35. }
  36. // Infrastructure 基础设施结构
  37. type Infrastructure struct {
  38. dbExecutor database.Executor
  39. localCache cache.Cache
  40. redisCache cache.Cache
  41. redisMessageQueue messageQueueCommon.MessageQueue
  42. }
  43. func NewInfrastructure(config Config) *Infrastructure {
  44. i := new(Infrastructure)
  45. // 数据库执行器多选一
  46. if config.DatabaseConfig.Operations != nil {
  47. op, err := operations.NewOperations(config.DatabaseConfig.Operations)
  48. if err != nil {
  49. panic("创建数据库操作失败: " + err.Error())
  50. }
  51. i.dbExecutor = op
  52. } else if config.DatabaseConfig.DataService != nil {
  53. executor, err := data_service.NewExecutor(config.DatabaseConfig.DataService)
  54. if err != nil {
  55. panic("创建数据服务执行器失败: " + err.Error())
  56. }
  57. i.dbExecutor = executor
  58. }
  59. // 初始化缓存
  60. if strutils.IsStringNotEmpty(config.CacheConfig.Namespace) {
  61. namespace := config.CacheConfig.Namespace
  62. i.localCache = local.New(namespace)
  63. if config.CacheConfig.Redis != nil {
  64. redisConf := config.CacheConfig.Redis
  65. newRedisCache, err := redisCache.New(redisConf.Address, redisConf.UserName, redisConf.Password, redisConf.DB, namespace)
  66. if err != nil {
  67. panic("初始化Redis缓存失败: " + err.Error())
  68. }
  69. i.redisCache = newRedisCache
  70. }
  71. }
  72. // 初始化Redis消息队列
  73. if config.MessageQueueConfig.Redis != nil {
  74. redisConf := config.MessageQueueConfig.Redis
  75. maxLen := config.MessageQueueConfig.MaxLen
  76. newRedisMessageQueue := redisMessageQueue.New(redisConf.Address, redisConf.UserName, redisConf.Password, redisConf.DB,
  77. messageQueueCommon.WithMaxLen(maxLen))
  78. i.redisMessageQueue = newRedisMessageQueue
  79. }
  80. return i
  81. }
  82. func DestroyInfrastructure(i *Infrastructure) {
  83. if i == nil {
  84. return
  85. }
  86. if i.redisMessageQueue != nil {
  87. redisMessageQueue.Destroy(i.redisMessageQueue.(*redisMessageQueue.MessageQueue))
  88. }
  89. if i.redisCache != nil {
  90. err := redisCache.Destroy(i.redisCache.(*redisCache.Cache))
  91. if err != nil {
  92. panic("销毁Redis缓存失败: " + err.Error())
  93. }
  94. }
  95. if i.localCache != nil {
  96. local.Destroy(i.localCache.(*local.Cache))
  97. }
  98. if i.dbExecutor != nil {
  99. switch dbExecutor := i.dbExecutor.(type) {
  100. case *operations.Operations:
  101. err := operations.DestroyOperation(dbExecutor)
  102. if err != nil {
  103. panic("销毁数据库操作失败: " + err.Error())
  104. }
  105. case *data_service.Executor:
  106. err := data_service.DestroyExecutor(dbExecutor)
  107. if err != nil {
  108. panic("销毁数据库操作失败: " + err.Error())
  109. }
  110. default:
  111. panic("不支持的数据库执行器类型")
  112. }
  113. }
  114. return
  115. }
  116. // DBExecutor 获取数据库基础设施
  117. // 参数: 无
  118. // 返回值:
  119. // - 数据库基础设施的接口
  120. func (i Infrastructure) DBExecutor() database.Executor {
  121. return i.dbExecutor
  122. }
  123. // LocalCache 获取本地缓存基础设施
  124. // 参数: 无
  125. // 返回值:
  126. // - 缓存基础设施的接口
  127. func (i Infrastructure) LocalCache() cache.Cache {
  128. return i.localCache
  129. }
  130. // RedisCache 获取Redis缓存基础设施
  131. // 参数: 无
  132. // 返回值:
  133. // - 缓存基础设施的接口
  134. func (i Infrastructure) RedisCache() cache.Cache {
  135. return i.redisCache
  136. }