package data import ( "git.gz.internal.jumaiyx.cn/jm/jmproto/conf" "git.gz.internal.jumaiyx.cn/job/room-mike-hot-timer/internal/data/model" "git.gz.internal.jumaiyx.cn/job/room-mike-hot-timer/pkg/redis" "git.gz.internal.jumaiyx.cn/pkg/log" "github.com/google/wire" _ "github.com/jackc/pgx/v4/stdlib" "gorm.io/driver/postgres" "gorm.io/gorm" "time" ) // ProviderSet is data providers. var ProviderSet = wire.NewSet( NewData, NewRoomMikeRepo, NewInitValue, ) // Data . type Data struct { db *gorm.DB cache *redis.Cache } // NewData . func NewData(c *conf.Data, log log.Logger) (*Data, func(), error) { cleanup := func() { log.Info("closing the data resources") } dsn := c.Database.Source client, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { log.Errorf("failed creating schema resources: %v", err) return nil, nil, err } sqlDB, err := client.DB() if err != nil { log.Errorf("failed creating schema resources: %v", err) return nil, nil, err } // SetMaxIdleConns 设置空闲连接池中连接的最大数量 sqlDB.SetMaxIdleConns(10) // SetMaxOpenConns 设置打开数据库连接的最大数量 sqlDB.SetMaxOpenConns(100) // SetConnMaxLifetime 设置了连接可复用的最大时间 sqlDB.SetConnMaxLifetime(time.Hour) cache := redis.NewCache(log, c.Redis) err = model.AutoMigrate(client, cache) if err != nil { log.Errorf("failed creating schema resources: %v", err) return nil, nil, err } return &Data{ db: client, cache: cache, // redis }, cleanup, nil }