infrastructure.go 3.6 KB

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