Posts

gin参数校验

May 8, 2024
Go, Gin

第三方包 # github.com/go-playground/validator/v10 常用参数 # required //必填; len=11 //长度=11; min=3 //如果是数字,验证的是数据大小范围,最小值为3,如果是文本,验证的是最小长度为3, max=6 //如果是数字,验证的是数字最大值为6,如果是文本,验证的是最大长度为6 mail //验证邮箱 gt=3 //对于文本就是长度>=3 lt=6 //对于文本就是长度<=6 翻译中间件 # package middleware import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/locales/en" "github.com/go-playground/locales/zh" ut "github.com/go-playground/universal-translator" validator "github.com/go-playground/validator/v10" enTranslations "github.com/go-playground/validator/v10/translations/en" zhTranslations "github.com/go-playground/validator/v10/translations/zh" ) func Translations() gin.HandlerFunc { return func(c *gin.Context) { //locale := .GetHeader("Acept-Language") locale := "zh" uni := ut.New(en.New(), zh.New()) trans, _ := uni.GetTranslator(locale) v, ok := binding.Validator.Engine().(*validator.Validate) if ok { switch locale { case "zh": _ = zhTranslations. ...

gorm自制日志

May 8, 2024
Go, Gorm, Mysql, Log

第三方包 # gorm.io/gorm/logger 示例 # package boot import ( "context" "fmt" "io/ioutil" "log" "os" "time" "github.com/xiaohubai/alpha/config" "go.uber.org/zap" "gorm.io/gorm/logger" "gorm.io/gorm/utils" ) type cfg struct { SlowThreshold time.Duration Colorful bool LogLevel logger.LogLevel } type traceRecorder struct { logger.Interface BeginAt time.Time SQL string RowsAffected int64 Err error } func (t traceRecorder) New() *traceRecorder { return &traceRecorder{Interface: t.Interface, BeginAt: time.Now()} } func (t *traceRecorder) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { t. ...

go接口方法重载

May 8, 2024
Go

源码 # package main import "fmt" type Log interface { Error() } type LocalLog struct { Level string `json:"level"` Key string `json:"key"` Msg string `json:"msg"` TraceId string `json:"trace_id"` } type Atta struct { Level string `json:"level"` Key string `json:"key"` Msg string `json:"msg"` TraceId string `json:"trace_id"` AttaId string `json:"atta_id"` } func CommError(obj Log) { obj.Error() } func (l *LocalLog) Error() { fmt.Println(l.Level, l.TraceId) } func (a *Atta) Error() { fmt.Println(a.Level, a. ...

go项目分层设计

May 8, 2024
Go

单系统 Project Layout 介绍 # ├── api (API) │ └── v1 (版本) ├── service (业务层) ├── model (结构体层) ├── dao (数据库操作) ├── middleware (中间件) ├── config (全局配置) ├── boot (初始组件) ├── router (路由控制) ├── plugin (组件具体实现) ├── utils (工具) ├── script (脚本) ├── docs (文档) ├── public (静态文件) ├── log (日志) ├── main.go (入口) ├── go.mod (项目依赖包版本) ├── go.sum (已下载的所有依赖版本) ├── Dockerfile (docker部署文件) └── README.md (项目说明) 大仓微服务 project layout # ├── api //所有proto文件定义,外部引用 │ └── user │ ├── user. ...

redis内存满了怎么办?

May 8, 2024
Redis

maxmemory # redis 设置最大使用内存 maxmemory,默认资源超过maxmemory后,不允许新key加入 7种淘汰策略 # volatile-lru:淘汰那些设置了过期时间且最近最少被访问的数据 volatile-random:随机淘汰,腾出坑位给新人; volatile-ttl:淘汰设置了过期时间key,谁最接近时间就先淘汰谁 allkeys-lru:淘汰最近最少上一线干活的职员; allkeys-lfu:淘汰最少上一线干活的公务员; allkeys-random:随机淘汰职员,为新兵腾出空位。 淘汰执行过程 # 客户端发送新命令到服务端; 服务端收到客户端命令,Redis 检查内存使用情况,如果大于 maxmemory 限制,则根据策略驱逐数据。执行新命令,否则执行新命令 使用场景 # allkeys-lru 使用场景有明显的冷热数据区分,充分利用 LRU 算法把最近最常访问的数据保留,有限的内存提高访问性能 allkeys-random 使用场景数据没有明显的冷热分别,所有的数据分布查询比较均衡,让其随机选择淘汰数据 volatile-lru 业务场景有一些数据不能删除,比如置顶新闻、视频,这时候我们为这些数据不设置过期时间,这样的话数据就不会被删除,该策略就会去根据 LRU 算法去淘汰那些设置了过期时间且最近最少被访问的数据

sql语句性能优化

May 8, 2024
Mysql

1、对查询进行优化,应尽量避免全表扫描,首先应考虑在 WHERE 及 ORDER BY 涉及的列上建立索引。 2、应尽量避免在 WHERE 子句中对字段进行 NULL 值判断,创建表时 NULL 是默认值,但大多数时候应该使用 NOT NULL,或者使用一个特殊的值,如 0,-1 作为默认值。 3、应尽量避免在 WHERE 子句中使用 != 或 <> 操作符。MySQL 只有对以下操作符才使用索引:<,<=,=,>,>=,BETWEEN,IN,以及某些时候的 LIKE。 4、应尽量避免在 WHERE 子句中使用 OR 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,可以使用 UNION 合并查询:select id from t where num=10 union all select id from t where num=20。 5、IN 和 NOT IN 也要慎用,否则会导致全表扫描。对于连续的数值,能用 BETWEEN 就不要用 IN:select id from t where num between 1 and 3。、 6、下面的查询也将导致全表扫描:select id from t where name like‘%abc%’ 或者select id from t where name like‘%abc’若要提高效率,可以考虑全文检索。而select id from t where name like‘abc%’才用到索引。 7、如果在 WHERE 子句中使用参数,也会导致全表扫描。 8、应尽量避免在 WHERE 子句中对字段进行表达式操作,应尽量避免在 WHERE 子句中对字段进行函数操作。 9、很多时候用 EXISTS 代替 IN 是一个好的选择:select num from a where num in(select num from b)。用下面的语句替换:select num from a where exists(select 1 from b where num=a. ...

wsl2固定ip

May 8, 2024
Wsl2

解决wsl2重启 内网ip发生变化 # @echo off setlocal enabledelayedexpansion wsl --shutdown ::重新拉起来,并且用root的身份,启动ssh服务和docker服务 wsl -u root service docker start | findstr "Starting Docker" > nul if !errorlevel! equ 0 ( echo docker start success :: 看看我要的IP在不在 wsl -u root ip addr | findstr "172.21.0.2" > nul if !errorlevel! equ 0 ( echo wsl ip has set ) else ( wsl -u root ip addr add 172.21.0.2/24 broadcast 172.21.0.0 dev eth0 label eth0:1 echo set wsl ip success: 172. ...