infrastructure.go 3.0 KB

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