123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package infrastructure
- import (
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache/local"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/cache/redis"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/data_service"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/operations"
- "git.sxidc.com/go-tools/utils/strutils"
- )
- type Config struct {
- DatabaseConfig
- CacheConfig
- }
- type DatabaseConfig struct {
- Operations *operations.Config `json:"operations" yaml:"operations"`
- DataService *data_service.Config `json:"data_service" yaml:"data_service"`
- }
- type CacheConfig struct {
- Namespace string `json:"namespace" yaml:"namespace"`
- Redis *RedisConfig `json:"redis" yaml:"redis"`
- }
- type RedisConfig struct {
- Address string `json:"address" yaml:"address"`
- UserName string `json:"user_name" yaml:"user_name"`
- Password string `json:"password" yaml:"password"`
- DB int `json:"db" yaml:"db"`
- }
- // Infrastructure 基础设施结构
- type Infrastructure struct {
- dbExecutor database.Executor
- localCache cache.Cache
- redisCache cache.Cache
- }
- func NewInfrastructure(config Config) *Infrastructure {
- i := new(Infrastructure)
- // 数据库执行器多选一
- if config.DatabaseConfig.Operations != nil {
- op, err := operations.NewOperations(config.DatabaseConfig.Operations)
- if err != nil {
- panic("创建数据库操作失败: " + err.Error())
- }
- i.dbExecutor = op
- } else if config.DatabaseConfig.DataService != nil {
- executor, err := data_service.NewExecutor(config.DatabaseConfig.DataService)
- if err != nil {
- panic("创建数据服务执行器失败: " + err.Error())
- }
- i.dbExecutor = executor
- }
- // 初始化缓存
- if strutils.IsStringNotEmpty(config.CacheConfig.Namespace) {
- namespace := config.CacheConfig.Namespace
- i.localCache = local.New(namespace)
- if config.CacheConfig.Redis != nil {
- redisConf := config.CacheConfig.Redis
- redisCache, err := redis.New(redisConf.Address, redisConf.UserName, redisConf.Password, redisConf.DB, namespace)
- if err != nil {
- panic("初始化Redis缓存失败: " + err.Error())
- }
- i.redisCache = redisCache
- }
- }
- return i
- }
- func DestroyInfrastructure(i *Infrastructure) {
- if i == nil {
- return
- }
- if i.redisCache != nil {
- err := redis.Destroy(i.redisCache.(*redis.Cache))
- if err != nil {
- panic("销毁Redis缓存失败: " + err.Error())
- }
- }
- if i.localCache != nil {
- local.Destroy(i.localCache.(*local.Cache))
- }
- if i.dbExecutor != nil {
- switch dbExecutor := i.dbExecutor.(type) {
- case *operations.Operations:
- err := operations.DestroyOperation(dbExecutor)
- if err != nil {
- panic("销毁数据库操作失败: " + err.Error())
- }
- case *data_service.Executor:
- err := data_service.DestroyExecutor(dbExecutor)
- if err != nil {
- panic("销毁数据库操作失败: " + err.Error())
- }
- default:
- panic("不支持的数据库执行器类型")
- }
- }
- return
- }
- // DBExecutor 获取数据库基础设施
- // 参数: 无
- // 返回值:
- // - 数据库基础设施的接口
- func (i Infrastructure) DBExecutor() database.Executor {
- return i.dbExecutor
- }
- // LocalCache 获取本地缓存基础设施
- // 参数: 无
- // 返回值:
- // - 缓存基础设施的接口
- func (i Infrastructure) LocalCache() cache.Cache {
- return i.localCache
- }
- // RedisCache 获取Redis缓存基础设施
- // 参数: 无
- // 返回值:
- // - 缓存基础设施的接口
- func (i Infrastructure) RedisCache() cache.Cache {
- return i.redisCache
- }
|