mirror of
https://github.com/ProudMuBai/GoFilm.git
synced 2026-02-15 23:24:41 +08:00
init
This commit is contained in:
30
server/plugin/db/mysql.go
Normal file
30
server/plugin/db/mysql.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
"server/config"
|
||||
)
|
||||
|
||||
var Mdb *gorm.DB
|
||||
|
||||
func InitMysql() (err error) {
|
||||
// client 相关属性设置
|
||||
Mdb, err = gorm.Open(mysql.New(mysql.Config{
|
||||
DSN: config.MysqlDsn,
|
||||
DefaultStringSize: 255, //string类型字段默认长度
|
||||
DisableDatetimePrecision: true, // 禁用 datetime 精度
|
||||
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式
|
||||
DontSupportRenameColumn: true, // 用change 重命名列
|
||||
SkipInitializeWithVersion: false, // 根据当前Mysql版本自动配置
|
||||
}), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
//TablePrefix: "t_", //设置创建表时的前缀
|
||||
SingularTable: true, //是否使用 结构体名称作为表名 (关闭自动变复数)
|
||||
//NameReplacer: strings.NewReplacer("spider_", ""), // 替表名和字段中的 Me 为 空
|
||||
},
|
||||
//Logger: logger.Default.LogMode(logger.Info), //设置日志级别为Info
|
||||
})
|
||||
return
|
||||
}
|
||||
37
server/plugin/db/redis.go
Normal file
37
server/plugin/db/redis.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"server/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
/*
|
||||
redis 工具类
|
||||
*/
|
||||
var Rdb *redis.Client
|
||||
var Cxt = context.Background()
|
||||
|
||||
// InitRedisConn 初始化redis客户端
|
||||
func InitRedisConn() error {
|
||||
|
||||
Rdb = redis.NewClient(&redis.Options{
|
||||
Addr: config.RedisAddr,
|
||||
Password: config.RedisPassword,
|
||||
DB: config.RedisDBNo,
|
||||
PoolSize: 10, // 默认连接数
|
||||
DialTimeout: time.Second * 10, // 超时时间
|
||||
})
|
||||
// 测试连接是否正常
|
||||
_, err := Rdb.Ping(Cxt).Result()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 关闭redis连接
|
||||
func CloseRedis() error {
|
||||
return Rdb.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user