From f52feef2e8a02b228b1fdbb83e74133818ce9243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Fri, 27 May 2022 13:16:47 +0800 Subject: [PATCH] =?UTF-8?q?-=20mysql=EF=BC=9A=E6=9B=B4=E6=96=B0=20-=20post?= =?UTF-8?q?gres=EF=BC=9A=E6=9B=B4=E6=96=B0=20-=20redis=EF=BC=9A=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++ utils/gomysql/{app.go => gomysql.go} | 26 ++++++++++--- .../app.go => gopostgres/gopostgres.go} | 27 +++++++++---- utils/{gopostgresql => gopostgres}/type.go | 2 +- utils/goredis/app.go | 7 +++- utils/goredis/simple_interface_cache.go | 20 ++++------ utils/goredis/simple_operation.go | 38 +++++++++++++++++++ utils/goredis/simple_result.go | 35 +++++++++++++++++ 8 files changed, 130 insertions(+), 28 deletions(-) rename utils/gomysql/{app.go => gomysql.go} (81%) rename utils/{gopostgresql/app.go => gopostgres/gopostgres.go} (80%) rename utils/{gopostgresql => gopostgres}/type.go (96%) create mode 100644 utils/goredis/simple_operation.go create mode 100644 utils/goredis/simple_result.go diff --git a/CHANGELOG.md b/CHANGELOG.md index eb7e7102..45371297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ - ip:更新 - storage:增加存储功能 - request:合并http +- mysql:更新 +- postgres:更新 +- redis:更新 ## v1.0.39 / 2021-12-27 diff --git a/utils/gomysql/app.go b/utils/gomysql/gomysql.go similarity index 81% rename from utils/gomysql/app.go rename to utils/gomysql/gomysql.go index 928c0c37..7def4992 100644 --- a/utils/gomysql/app.go +++ b/utils/gomysql/gomysql.go @@ -6,25 +6,32 @@ import ( "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" + "log" "os" "path" "time" ) type App struct { - Db *gorm.DB // 驱动 - Dns string // 地址 - Log bool // 日志 + Db *gorm.DB // 驱动 + Dns string // 地址 + Log bool // 日志 + LogUrl string // 日志路径 } type writer struct{} +// 日志路径 +var logsUrl = "" + func (w writer) Printf(format string, args ...interface{}) { + // 判断路径 + now := time.Now() logFilePath := "" if dir, err := os.Getwd(); err == nil { - logFilePath = dir + "/logs/mysql" + logFilePath = dir + logsUrl } if err := os.MkdirAll(logFilePath, 0777); err != nil { fmt.Println(err.Error()) @@ -60,6 +67,13 @@ func (w writer) Printf(format string, args ...interface{}) { func (app *App) InitClient() { + log.Printf("mysql config:%+v\n", app) + + // 判断路径 + if app.LogUrl == "" { + logsUrl = "/logs/mysql" + } + var err error if app.Log == true { @@ -79,12 +93,12 @@ func (app *App) InitClient() { } if err != nil { - panic(fmt.Sprintf("连接数据库失败:%v", err)) + panic(fmt.Sprintf("数据库【mysql】连接失败:%v", err)) } sqlDB, err := app.Db.DB() if err != nil { - panic(fmt.Sprintf("连接数据库服务器失败:%v", err)) + panic(fmt.Sprintf("数据库【mysql】连接服务器失败:%v", err)) } sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量 diff --git a/utils/gopostgresql/app.go b/utils/gopostgres/gopostgres.go similarity index 80% rename from utils/gopostgresql/app.go rename to utils/gopostgres/gopostgres.go index 5897367a..5094364d 100644 --- a/utils/gopostgresql/app.go +++ b/utils/gopostgres/gopostgres.go @@ -1,4 +1,4 @@ -package gopostgresql +package gopostgres import ( "fmt" @@ -6,25 +6,30 @@ import ( "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/gorm/logger" + "log" "os" "path" "time" ) type App struct { - Db *gorm.DB // 驱动 - Dns string // 地址 - Log bool // 日志 + Db *gorm.DB // 驱动 + Dns string // 地址 + Log bool // 日志 + LogUrl string // 日志路径 } type writer struct{} +// 日志路径 +var logsUrl = "" + func (w writer) Printf(format string, args ...interface{}) { now := time.Now() logFilePath := "" if dir, err := os.Getwd(); err == nil { - logFilePath = dir + "/logs/postgresql" + logFilePath = dir + logsUrl } if err := os.MkdirAll(logFilePath, 0777); err != nil { fmt.Println(err.Error()) @@ -59,6 +64,14 @@ func (w writer) Printf(format string, args ...interface{}) { } func (app *App) InitClient() { + + log.Printf("pgsql config:%+v\n", app) + + // 判断路径 + if app.LogUrl == "" { + logsUrl = "/logs/postgresql" + } + var err error if app.Log == true { @@ -78,12 +91,12 @@ func (app *App) InitClient() { } if err != nil { - panic(fmt.Sprintf("连接数据库失败:%v", err)) + panic(fmt.Sprintf("数据库【pgsql】连接失败:%v", err)) } sqlDB, err := app.Db.DB() if err != nil { - panic(fmt.Sprintf("连接数据库服务器失败:%v", err)) + panic(fmt.Sprintf("数据库【pgsql】连接服务器失败:%v", err)) } sqlDB.SetMaxIdleConns(10) // 设置空闲连接池中连接的最大数量 diff --git a/utils/gopostgresql/type.go b/utils/gopostgres/type.go similarity index 96% rename from utils/gopostgresql/type.go rename to utils/gopostgres/type.go index 62cd6bc2..529a49d4 100644 --- a/utils/gopostgresql/type.go +++ b/utils/gopostgres/type.go @@ -1,4 +1,4 @@ -package gopostgresql +package gopostgres import ( "database/sql/driver" diff --git a/utils/goredis/app.go b/utils/goredis/app.go index c955dac1..38630163 100644 --- a/utils/goredis/app.go +++ b/utils/goredis/app.go @@ -20,10 +20,13 @@ type App struct { // InitClient 初始化连接 func (app *App) InitClient() { + + log.Printf("redis config:%+v\n", app) + if app.PoolSize == 0 { app.PoolSize = 100 } - log.Printf("goredis:%+v\n", app) + app.Db = redis.NewClient(&redis.Options{ Addr: app.Addr, // 地址 Password: app.Password, // 密码 @@ -36,7 +39,7 @@ func (app *App) InitClient() { _, err := app.Db.Ping(ctx).Result() if err != nil { - panic(errors.New(fmt.Sprintf("goredis ping error:%v", err))) + panic(errors.New(fmt.Sprintf("数据库【redis】连接失败:%v", err))) } return } diff --git a/utils/goredis/simple_interface_cache.go b/utils/goredis/simple_interface_cache.go index 246cc25f..c80d2469 100644 --- a/utils/goredis/simple_interface_cache.go +++ b/utils/goredis/simple_interface_cache.go @@ -1,7 +1,7 @@ package goredis import ( - "encoding/json" + "log" "time" ) @@ -9,13 +9,13 @@ type DBGttInterfaceFunc func() interface{} // SimpleInterfaceCache 缓存 type SimpleInterfaceCache struct { - Operation *StringOperation // 操作类 + Operation *SimpleOperation // 操作类 Expire time.Duration // 过期时间 DBGetter DBGttInterfaceFunc // 缓存不存在的操作 DB } // NewSimpleInterfaceCache 构造函数 -func (app *App) NewSimpleInterfaceCache(operation *StringOperation, expire time.Duration) *SimpleInterfaceCache { +func (app *App) NewSimpleInterfaceCache(operation *SimpleOperation, expire time.Duration) *SimpleInterfaceCache { return &SimpleInterfaceCache{ Operation: operation, // 操作类 Expire: expire, // 过期时间 @@ -28,16 +28,12 @@ func (c *SimpleInterfaceCache) SetCache(key string, value interface{}) { } // GetCache 获取缓存 -func (c *SimpleInterfaceCache) GetCache(key string) (ret string) { - f := func() string { - obj := c.DBGetter() - b, err := json.Marshal(obj) - if err != nil { - return "" - } - return string(b) +func (c *SimpleInterfaceCache) GetCache(key string) (ret interface{}) { + f := func() interface{} { + return c.DBGetter() } ret = c.Operation.Get(key).UnwrapOrElse(f) c.SetCache(key, ret) - return + log.Println(ret) + return ret } diff --git a/utils/goredis/simple_operation.go b/utils/goredis/simple_operation.go new file mode 100644 index 00000000..d479f9f9 --- /dev/null +++ b/utils/goredis/simple_operation.go @@ -0,0 +1,38 @@ +package goredis + +import ( + "context" + "github.com/go-redis/redis/v8" + "time" +) + +type SimpleOperation struct { + db *redis.Client + ctx context.Context +} + +func (app *App) NewSimpleOperation() *SimpleOperation { + return &SimpleOperation{ + db: app.Db, + ctx: context.Background(), + } +} + +// Set 设置 +func (o *SimpleOperation) Set(key string, value interface{}, attrs ...*OperationAttr) *SimpleResult { + exp := OperationAttrs(attrs).Find(AttrExpr) + if exp == nil { + exp = time.Second * 0 + } + return NewSimpleResult(o.db.Set(o.ctx, key, value, exp.(time.Duration)).Result()) +} + +// Get 获取单个 +func (o *SimpleOperation) Get(key string) *SimpleResult { + return NewSimpleResult(o.db.Get(o.ctx, key).Result()) +} + +// Del 删除key操作,支持批量删除 +func (o *SimpleOperation) Del(keys ...string) *redis.IntCmd { + return o.db.Del(o.ctx, keys...) +} diff --git a/utils/goredis/simple_result.go b/utils/goredis/simple_result.go new file mode 100644 index 00000000..25fbe0da --- /dev/null +++ b/utils/goredis/simple_result.go @@ -0,0 +1,35 @@ +package goredis + +type SimpleResult struct { + Result interface{} + Err error +} + +// NewSimpleResult 构造函数 +func NewSimpleResult(result interface{}, err error) *SimpleResult { + return &SimpleResult{Result: result, Err: err} +} + +// Unwrap 空值情况下返回错误 +func (r *SimpleResult) Unwrap() interface{} { + if r.Err != nil { + panic(r.Err) + } + return r.Result +} + +// UnwrapOr 空值情况下设置返回默认值 +func (r *SimpleResult) UnwrapOr(defaults interface{}) interface{} { + if r.Err != nil { + return defaults + } + return r.Result +} + +// UnwrapOrElse 空值情况下设置返回其他 +func (r *SimpleResult) UnwrapOrElse(f func() interface{}) interface{} { + if r.Err != nil { + return f() + } + return r.Result +}