infrastructure.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. type Infrastructure struct {
  29. dbExecutor database.Executor
  30. localCache cache.Cache
  31. redisCache cache.Cache
  32. }
  33. func NewInfrastructure(config Config) *Infrastructure {
  34. i := new(Infrastructure)
  35. // 数据库执行器多选一
  36. if config.DatabaseConfig.Operations != nil {
  37. op, err := operations.NewOperations(config.DatabaseConfig.Operations)
  38. if err != nil {
  39. panic("创建数据库操作失败: " + err.Error())
  40. }
  41. i.dbExecutor = op
  42. } else if config.DatabaseConfig.DataService != nil {
  43. dataService, err := data_service.NewDataService(config.DatabaseConfig.DataService)
  44. if err != nil {
  45. panic("创建数据服务失败: " + err.Error())
  46. }
  47. i.dbExecutor = dataService
  48. }
  49. // 初始化缓存
  50. if strutils.IsStringNotEmpty(config.CacheConfig.Namespace) {
  51. namespace := config.CacheConfig.Namespace
  52. i.localCache = local.New(namespace)
  53. if config.CacheConfig.Redis != nil {
  54. redisConf := config.CacheConfig.Redis
  55. redisCache, err := redis.New(redisConf.Address, redisConf.UserName, redisConf.Password, redisConf.DB, namespace)
  56. if err != nil {
  57. panic("初始化Redis缓存失败: " + err.Error())
  58. }
  59. i.redisCache = redisCache
  60. }
  61. }
  62. return i
  63. }
  64. func DestroyInfrastructure(i *Infrastructure) {
  65. if i == nil {
  66. return
  67. }
  68. if i.redisCache != nil {
  69. err := redis.Destroy(i.redisCache.(*redis.Cache))
  70. if err != nil {
  71. panic("销毁Redis缓存失败: " + err.Error())
  72. }
  73. }
  74. if i.localCache != nil {
  75. local.Destroy(i.localCache.(*local.Cache))
  76. }
  77. if i.dbExecutor != nil {
  78. switch dbExecutor := i.dbExecutor.(type) {
  79. case *operations.Operations:
  80. err := operations.DestroyOperation(dbExecutor)
  81. if err != nil {
  82. panic("销毁数据库操作失败: " + err.Error())
  83. }
  84. case *data_service.DataService:
  85. err := data_service.DestroyDataService(dbExecutor)
  86. if err != nil {
  87. panic("销毁数据服务失败: " + err.Error())
  88. }
  89. default:
  90. panic("不支持的数据库执行器类型")
  91. }
  92. }
  93. return
  94. }
  95. func (i Infrastructure) DBExecutor() database.Executor {
  96. return i.dbExecutor
  97. }
  98. func (i Infrastructure) LocalCache() cache.Cache {
  99. return i.localCache
  100. }
  101. func (i Infrastructure) RedisCache() cache.Cache {
  102. return i.redisCache
  103. }