infrastructure.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. }
  29. type RedisConfig struct {
  30. Address string `json:"address" yaml:"address"`
  31. UserName string `json:"user_name" yaml:"user_name"`
  32. Password string `json:"password" yaml:"password"`
  33. DB int `json:"db" yaml:"db"`
  34. MaxLen int64 `json:"max_len" yaml:"max_len"`
  35. ConsumerNum int `json:"consumer_num" yaml:"consumer_num"`
  36. }
  37. // Infrastructure 基础设施结构
  38. type Infrastructure struct {
  39. dbExecutor database.Executor
  40. localCache cache.Cache
  41. redisCache cache.Cache
  42. redisMessageQueue messageQueueCommon.MessageQueue
  43. }
  44. func NewInfrastructure(config Config) *Infrastructure {
  45. i := new(Infrastructure)
  46. // 数据库执行器多选一
  47. if config.DatabaseConfig.Operations != nil {
  48. op, err := operations.NewOperations(config.DatabaseConfig.Operations)
  49. if err != nil {
  50. panic("创建数据库操作失败: " + err.Error())
  51. }
  52. i.dbExecutor = op
  53. } else if config.DatabaseConfig.DataService != nil {
  54. executor, err := data_service.NewExecutor(config.DatabaseConfig.DataService)
  55. if err != nil {
  56. panic("创建数据服务执行器失败: " + err.Error())
  57. }
  58. i.dbExecutor = executor
  59. }
  60. // 初始化缓存
  61. if strutils.IsStringNotEmpty(config.CacheConfig.Namespace) {
  62. namespace := config.CacheConfig.Namespace
  63. i.localCache = local.New(namespace)
  64. if config.CacheConfig.Redis != nil {
  65. redisConf := config.CacheConfig.Redis
  66. newRedisCache, err := redisCache.New(redisConf.Address, redisConf.UserName, redisConf.Password, redisConf.DB, namespace)
  67. if err != nil {
  68. panic("初始化Redis缓存失败: " + err.Error())
  69. }
  70. i.redisCache = newRedisCache
  71. }
  72. }
  73. // 初始化Redis消息队列
  74. if config.MessageQueueConfig.Redis != nil {
  75. redisConf := config.MessageQueueConfig.Redis
  76. newRedisMessageQueue := redisMessageQueue.New(redisConf.Address, redisConf.UserName, redisConf.Password, redisConf.DB,
  77. redisMessageQueue.WithMaxLen(redisConf.MaxLen),
  78. redisMessageQueue.WithConsumerNum(redisConf.ConsumerNum))
  79. i.redisMessageQueue = newRedisMessageQueue
  80. }
  81. return i
  82. }
  83. func DestroyInfrastructure(i *Infrastructure) {
  84. if i == nil {
  85. return
  86. }
  87. if i.redisMessageQueue != nil {
  88. redisMessageQueue.Destroy(i.redisMessageQueue.(*redisMessageQueue.MessageQueue))
  89. }
  90. if i.redisCache != nil {
  91. err := redisCache.Destroy(i.redisCache.(*redisCache.Cache))
  92. if err != nil {
  93. panic("销毁Redis缓存失败: " + err.Error())
  94. }
  95. }
  96. if i.localCache != nil {
  97. local.Destroy(i.localCache.(*local.Cache))
  98. }
  99. if i.dbExecutor != nil {
  100. switch dbExecutor := i.dbExecutor.(type) {
  101. case *operations.Operations:
  102. err := operations.DestroyOperation(dbExecutor)
  103. if err != nil {
  104. panic("销毁数据库操作失败: " + err.Error())
  105. }
  106. case *data_service.Executor:
  107. err := data_service.DestroyExecutor(dbExecutor)
  108. if err != nil {
  109. panic("销毁数据库操作失败: " + err.Error())
  110. }
  111. default:
  112. panic("不支持的数据库执行器类型")
  113. }
  114. }
  115. return
  116. }
  117. // DBExecutor 获取数据库基础设施
  118. // 参数: 无
  119. // 返回值:
  120. // - 数据库基础设施的接口
  121. func (i Infrastructure) DBExecutor() database.Executor {
  122. return i.dbExecutor
  123. }
  124. // LocalCache 获取本地缓存基础设施
  125. // 参数: 无
  126. // 返回值:
  127. // - 缓存基础设施的接口
  128. func (i Infrastructure) LocalCache() cache.Cache {
  129. return i.localCache
  130. }
  131. // RedisCache 获取Redis缓存基础设施
  132. // 参数: 无
  133. // 返回值:
  134. // - 缓存基础设施的接口
  135. func (i Infrastructure) RedisCache() cache.Cache {
  136. return i.redisCache
  137. }
  138. // RedisMessageQueue 获取Redis消息队列基础设施
  139. // 参数: 无
  140. // 返回值:
  141. // - 消息队列基础设施的接口
  142. func (i Infrastructure) RedisMessageQueue() messageQueueCommon.MessageQueue {
  143. return i.redisMessageQueue
  144. }