From 4254d020b9fe0bde63f0d7af7247cec0abaa9d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Thu, 15 Sep 2022 10:11:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(db):=E5=88=87=E6=8D=A2=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E9=A9=B1=E5=8A=A8=E4=B8=BApq=EF=BC=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8E=A7=E5=88=B6=E5=BA=93=E4=B8=BAbun?= =?UTF-8?q?=EF=BC=8C=E6=9B=B4=E6=94=B9Redis=E8=BF=9E=E6=8E=A5=E7=9A=84?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/abstract.go | 28 ++++++++++++------------- cache/relation.go | 28 ++++++++++++------------- global/db.go | 53 ++++++++++++++++++----------------------------- global/redis.go | 10 ++++----- 4 files changed, 53 insertions(+), 66 deletions(-) diff --git a/cache/abstract.go b/cache/abstract.go index 1135c8a..cedab1f 100644 --- a/cache/abstract.go +++ b/cache/abstract.go @@ -25,24 +25,24 @@ func Cache[T interface{}](key string, value *T, expires time.Duration) error { var err error if expires > 0 { realExpires := expires + time.Duration(rand.Int63n(60))*time.Second - setCmd := global.RedisConn.B().Set(). + setCmd := global.Rd.B().Set(). Key(key).Value(rueidis.JSON(value)). ExSeconds(int64(realExpires.Seconds())). Build() - err = global.RedisConn.Do(global.Ctx, setCmd).Error() + err = global.Rd.Do(global.Ctx, setCmd).Error() } else { - setCmd := global.RedisConn.B().Set(). + setCmd := global.Rd.B().Set(). Key(key).Value(rueidis.JSON(value)). Build() - err = global.RedisConn.Do(global.Ctx, setCmd).Error() + err = global.Rd.Do(global.Ctx, setCmd).Error() } return err } // 从Redis缓存中获取一个数据 func Retreive[T interface{}](key string) (*T, error) { - getCmd := global.RedisConn.B().Get().Key(key).Build() - result := global.RedisConn.Do(global.Ctx, getCmd) + getCmd := global.Rd.B().Get().Key(key).Build() + result := global.Rd.Do(global.Ctx, getCmd) if result.Error() != nil { if rueidis.IsRedisNil(result.Error()) { return nil, nil @@ -60,8 +60,8 @@ func Retreive[T interface{}](key string) (*T, error) { // 检查Redis缓存中是否存在指定键的记录 func Exists(key string) (bool, error) { - existsCmd := global.RedisConn.B().Exists().Key(key).Build() - result := global.RedisConn.Do(global.Ctx, existsCmd) + existsCmd := global.Rd.B().Exists().Key(key).Build() + result := global.Rd.Do(global.Ctx, existsCmd) if result.Error() != nil { return false, result.Error() } @@ -72,8 +72,8 @@ func Exists(key string) (bool, error) { // 从Redis缓存中删除指定键 // ! 如果指定键已不存在,那么本函数一样会返回false func Delete(key string) (bool, error) { - deleteCmd := global.RedisConn.B().Del().Key(key).Build() - result := global.RedisConn.Do(global.Ctx, deleteCmd) + deleteCmd := global.Rd.B().Del().Key(key).Build() + result := global.Rd.Do(global.Ctx, deleteCmd) if result.Error() != nil { return false, result.Error() } @@ -111,8 +111,8 @@ func DeleteAll(pattern string) error { sKeys []string ) for { - scanCmd := global.RedisConn.B().Scan().Cursor(cursor).Match(pattern).Count(20).Build() - results := global.RedisConn.Do(global.Ctx, scanCmd) + scanCmd := global.Rd.B().Scan().Cursor(cursor).Match(pattern).Count(20).Build() + results := global.Rd.Do(global.Ctx, scanCmd) cursor, sKeys, err = dissembleScan(results) if err != nil { return err @@ -123,8 +123,8 @@ func DeleteAll(pattern string) error { } } - delCmd := global.RedisConn.B().Del().Key(keys...).Build() - err = global.RedisConn.Do(global.Ctx, delCmd).Error() + delCmd := global.Rd.B().Del().Key(keys...).Build() + err = global.Rd.Do(global.Ctx, delCmd).Error() return err } diff --git a/cache/relation.go b/cache/relation.go index f50f5be..d707edb 100644 --- a/cache/relation.go +++ b/cache/relation.go @@ -32,16 +32,16 @@ func assembleRelationIdentity(storeType, key string, field ...string) string { func CacheRelation(relationName, storeType, key string, field ...string) error { relationKey := assembleRelationKey(relationName) relationIdentity := assembleRelationIdentity(storeType, key, field...) - cmd := global.RedisConn.B().Sadd().Key(relationKey).Member(relationIdentity).Build() - result := global.RedisConn.Do(global.Ctx, cmd) + cmd := global.Rd.B().Sadd().Key(relationKey).Member(relationIdentity).Build() + result := global.Rd.Do(global.Ctx, cmd) return result.Error() } // 从缓存中清理指定的关联键 func AbolishRelation(relationName string) error { relationKey := assembleRelationKey(relationName) - cmd := global.RedisConn.B().Smembers().Key(relationKey).Build() - relationItems, err := global.RedisConn.Do(global.Ctx, cmd).AsStrSlice() + cmd := global.Rd.B().Smembers().Key(relationKey).Build() + relationItems, err := global.Rd.Do(global.Ctx, cmd).AsStrSlice() if err != nil { return err } @@ -50,17 +50,17 @@ func AbolishRelation(relationName string) error { separated := strings.Split(item, ";") switch separated[0] { case STORE_TYPE_KEY: - cmd := global.RedisConn.B().Del().Key(separated[1]).Build() + cmd := global.Rd.B().Del().Key(separated[1]).Build() cmds = append(cmds, cmd) case STORE_TYPE_HASH: - cmd := global.RedisConn.B().Hdel().Key(separated[1]).Field(separated[2:]...).Build() + cmd := global.Rd.B().Hdel().Key(separated[1]).Field(separated[2:]...).Build() cmds = append(cmds, cmd) case STORE_TYPE_SET: - cmd := global.RedisConn.B().Srem().Key(separated[1]).Member(separated[2:]...).Build() + cmd := global.Rd.B().Srem().Key(separated[1]).Member(separated[2:]...).Build() cmds = append(cmds, cmd) } } - errs := global.RedisConn.DoMulti(global.Ctx, cmds...) + errs := global.Rd.DoMulti(global.Ctx, cmds...) firstErr, has := lo.Find(errs, func(elem rueidis.RedisResult) bool { return elem.Error() != nil }) @@ -79,8 +79,8 @@ func ClearOrphanRelationItems() error { sKeys []string ) for { - scanCmd := global.RedisConn.B().Scan().Cursor(cursor).Match(fmt.Sprintf("%s:*", TAG_RELATION)).Count(20).Build() - results := global.RedisConn.Do(global.Ctx, scanCmd) + scanCmd := global.Rd.B().Scan().Cursor(cursor).Match(fmt.Sprintf("%s:*", TAG_RELATION)).Count(20).Build() + results := global.Rd.Do(global.Ctx, scanCmd) cursor, sKeys, err = dissembleScan(results) if err != nil { return err @@ -92,8 +92,8 @@ func ClearOrphanRelationItems() error { } var cmds = make(rueidis.Commands, 0) for _, key := range keys { - relationItemsCmd := global.RedisConn.B().Smembers().Key(key).Build() - results := global.RedisConn.Do(global.Ctx, relationItemsCmd) + relationItemsCmd := global.Rd.B().Smembers().Key(key).Build() + results := global.Rd.Do(global.Ctx, relationItemsCmd) relationItems, err := results.AsStrSlice() if err != nil { return err @@ -105,12 +105,12 @@ func ClearOrphanRelationItems() error { return err } if !exist { - cmd := global.RedisConn.B().Srem().Key(key).Member(item).Build() + cmd := global.Rd.B().Srem().Key(key).Member(item).Build() cmds = append(cmds, cmd) } } } - errs := global.RedisConn.DoMulti(global.Ctx, cmds...) + errs := global.Rd.DoMulti(global.Ctx, cmds...) firstErr, has := lo.Find(errs, func(elem rueidis.RedisResult) bool { return elem.Error() != nil }) diff --git a/global/db.go b/global/db.go index e184457..5c36f3e 100644 --- a/global/db.go +++ b/global/db.go @@ -1,58 +1,45 @@ package global import ( + "database/sql" "fmt" - "strings" "time" "electricity_bill_calc/config" "electricity_bill_calc/logger" - // _ "github.com/lib/pq" - _ "github.com/jackc/pgx/v5/stdlib" - "xorm.io/xorm" - "xorm.io/xorm/log" + _ "github.com/lib/pq" + sqldblogger "github.com/simukti/sqldb-logger" + "github.com/simukti/sqldb-logger/logadapter/zapadapter" + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/pgdialect" ) var ( - DBConn *xorm.Engine + DB *bun.DB ) func SetupDatabaseConnection() error { var err error - // 以下连接方式是采用pgx驱动的时候使用的。 - DBConn, err = xorm.NewEngine("pgx", fmt.Sprintf( - "postgresql://%s:%s@%s:%d/%s?sslmode=disable&", + connStr := fmt.Sprintf( + "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai connect_timeout=0", + config.DatabaseSettings.Host, config.DatabaseSettings.User, config.DatabaseSettings.Pass, - config.DatabaseSettings.Host, - config.DatabaseSettings.Port, config.DatabaseSettings.DB, - )) - // 以下连接方式是采用lib/pq驱动的时候使用的。 - // DBConn, err = xorm.NewEngine("postgres", fmt.Sprintf( - // "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai connect_timeout=0", - // config.DatabaseSettings.Host, - // config.DatabaseSettings.User, - // config.DatabaseSettings.Pass, - // config.DatabaseSettings.DB, - // config.DatabaseSettings.Port, - // )) + config.DatabaseSettings.Port, + ) + db, err := sql.Open("postgres", connStr) if err != nil { return err } - xLogger := logger.NewXormZeroLogger(logger.GetLogger()) - DBConn.SetLogger(log.NewLoggerAdapter(xLogger)) - - DBConn.Ping() - DBConn.SetMaxIdleConns(config.DatabaseSettings.MaxIdleConns) - DBConn.SetMaxOpenConns(config.DatabaseSettings.MaxOpenConns) - DBConn.SetConnMaxLifetime(60 * time.Second) - - if strings.ToLower(config.ServerSettings.RunMode) == "debug" { - DBConn.ShowSQL(true) - DBConn.Logger().SetLevel(log.LOG_DEBUG) - } + db = sqldblogger.OpenDriver(connStr, db.Driver(), zapadapter.New(logger.Named("PG"))) + DB = bun.NewDB(db, pgdialect.New()) + DB.SetMaxIdleConns(config.DatabaseSettings.MaxIdleConns) + DB.SetMaxOpenConns(config.DatabaseSettings.MaxOpenConns) + DB.SetConnMaxIdleTime(10 * time.Minute) + DB.SetConnMaxLifetime(60 * time.Minute) + DB.Ping() return nil } diff --git a/global/redis.go b/global/redis.go index 9ceec58..e99c19f 100644 --- a/global/redis.go +++ b/global/redis.go @@ -9,13 +9,13 @@ import ( ) var ( - RedisConn rueidis.Client - Ctx = context.Background() + Rd rueidis.Client + Ctx = context.Background() ) func SetupRedisConnection() error { var err error - RedisConn, err = rueidis.NewClient(rueidis.ClientOption{ + Rd, err = rueidis.NewClient(rueidis.ClientOption{ InitAddress: []string{fmt.Sprintf("%s:%d", config.RedisSettings.Host, config.RedisSettings.Port)}, Password: config.RedisSettings.Password, SelectDB: config.RedisSettings.DB, @@ -23,8 +23,8 @@ func SetupRedisConnection() error { if err != nil { return err } - pingCmd := RedisConn.B().Ping().Build() - result := RedisConn.Do(Ctx, pingCmd) + pingCmd := Rd.B().Ping().Build() + result := Rd.Do(Ctx, pingCmd) if result.Error() != nil { return result.Error() }