- mysql:更新

- postgres:更新
- redis:更新
master
李光春 2 years ago
parent 9c6861a559
commit f52feef2e8

@ -14,6 +14,9 @@
- ip更新
- storage增加存储功能
- request合并http
- mysql更新
- postgres更新
- redis更新
## v1.0.39 / 2021-12-27

@ -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) // 设置空闲连接池中连接的最大数量

@ -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) // 设置空闲连接池中连接的最大数量

@ -1,4 +1,4 @@
package gopostgresql
package gopostgres
import (
"database/sql/driver"

@ -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
}

@ -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
}

@ -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...)
}

@ -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
}
Loading…
Cancel
Save