infrastructure.go 3.6 KB

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