infrastructure.go 2.7 KB

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