refactor(db):切换数据库驱动为pq,数据控制库为bun,更改Redis连接的名称。
This commit is contained in:
parent
769882dce5
commit
4254d020b9
28
cache/abstract.go
vendored
28
cache/abstract.go
vendored
@ -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
|
||||
}
|
||||
|
||||
|
28
cache/relation.go
vendored
28
cache/relation.go
vendored
@ -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
|
||||
})
|
||||
|
53
global/db.go
53
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
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user