optimize code, fix bug and upgrade unsafe package

This commit is contained in:
xing 2025-04-20 13:53:23 +08:00
parent 5dbf67f2f9
commit c96213ae1a
11 changed files with 24 additions and 30 deletions

View File

@ -81,7 +81,5 @@ go run app/cmd/main.go [-c configpath] [-p port]
用的gin框架和sqlx,在外面封装了层查询的方法。 用的gin框架和sqlx,在外面封装了层查询的方法。
#### 鸣谢
<a href="https://jb.gg/OpenSourceSupport"><img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png" alt="JetBrains Logo (Main) logo." width="30%"></a>

View File

@ -155,7 +155,7 @@ func IpLimit(num int64, clearNum ...int64) (func(ctx *gin.Context), func(int64,
a := NewFlowLimits(func(c *gin.Context) string { a := NewFlowLimits(func(c *gin.Context) string {
return c.ClientIP() return c.ClientIP()
}, ToManyRequest(), IpLimitClear) }, ToManyRequest(), IpLimitClear)
return CustomFlowLimit[string](a, num, clearNum...) return CustomFlowLimit(a, num, clearNum...)
} }
const minute = "2006-01-02 15:04" const minute = "2006-01-02 15:04"

12
cache/map.go vendored
View File

@ -300,11 +300,9 @@ func (m *MapCache[K, V]) getBatchToMap(e Expend[K, V]) func(c context.Context, k
} }
var needIndex = make(map[K]int) var needIndex = make(map[K]int)
res = mm res = mm
var flushKeys []K
for i, k := range key { for i, k := range key {
_, ok := mm[k] _, ok := mm[k]
if !ok { if !ok {
flushKeys = append(flushKeys, k)
needIndex[k] = i needIndex[k] = i
} }
} }
@ -354,7 +352,7 @@ func (m *MapCache[K, V]) getBatchToMap(e Expend[K, V]) func(c context.Context, k
}() }()
select { select {
case <-ctx.Done(): case <-ctx.Done():
err = errors.New(fmt.Sprintf("get cache %v %s", key, ctx.Err().Error())) err = fmt.Errorf("get cache %v %s", key, ctx.Err().Error())
return nil, err return nil, err
case <-done: case <-done:
} }
@ -421,7 +419,7 @@ func (m *MapCache[K, V]) getBatchToMapes(c context.Context, key []K, timeout tim
}() }()
select { select {
case <-ctx.Done(): case <-ctx.Done():
err = errors.New(fmt.Sprintf("get cache %v %s", key, ctx.Err().Error())) err = fmt.Errorf("get cache %v %s", key, ctx.Err().Error())
return nil, err return nil, err
case <-done: case <-done:
} }
@ -487,7 +485,7 @@ func (m *MapCache[K, V]) getCacheBatchs(c context.Context, key []K, timeout time
}() }()
select { select {
case <-ctx.Done(): case <-ctx.Done():
err = errors.New(fmt.Sprintf("get cache %v %s", key, ctx.Err().Error())) err = fmt.Errorf("get cache %v %s", key, ctx.Err().Error())
return nil, err return nil, err
case <-done: case <-done:
} }
@ -508,11 +506,9 @@ func (m *MapCache[K, V]) getBatches(e Expend[K, V]) func(ctx context.Context, ke
if err != nil { if err != nil {
return nil, err return nil, err
} }
var flushKeys []K
for i, k := range key { for i, k := range key {
v, ok := mm[k] v, ok := mm[k]
if !ok { if !ok {
flushKeys = append(flushKeys, k)
needIndex[k] = i needIndex[k] = i
var vv V var vv V
v = vv v = vv
@ -568,7 +564,7 @@ func (m *MapCache[K, V]) getBatches(e Expend[K, V]) func(ctx context.Context, ke
}() }()
select { select {
case <-ctx.Done(): case <-ctx.Done():
err = errors.New(fmt.Sprintf("get cache %v %s", key, ctx.Err().Error())) err = fmt.Errorf("get cache %v %s", key, ctx.Err().Error())
return nil, err return nil, err
case <-done: case <-done:
} }

8
cache/map_test.go vendored
View File

@ -3,12 +3,13 @@ package cache
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/taskPools"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/taskPools"
) )
var ca MapCache[string, string] var ca MapCache[string, string]
@ -67,7 +68,7 @@ func TestMapCache_Flush(t *testing.T) {
m MapCache[K, V] m MapCache[K, V]
args args args args
} }
ca := *NewMapCache[string, string](NewMemoryMapCache[string, string](func() time.Duration { ca := *NewMapCache(NewMemoryMapCache[string, string](func() time.Duration {
return time.Second return time.Second
}), fn, nil, nil, nil) }), fn, nil, nil, nil)
_, _ = ca.GetCache(ct, "aa", time.Second, ct, "aa") _, _ = ca.GetCache(ct, "aa", time.Second, ct, "aa")
@ -206,7 +207,6 @@ func TestMapCache_GetCacheBatch(t *testing.T) {
a, err := ca.GetCacheBatch(c, []string{"xx", "oo", "aa"}, time.Hour, c, []string{"xx", "oo", "aa"}) a, err := ca.GetCacheBatch(c, []string{"xx", "oo", "aa"}, time.Hour, c, []string{"xx", "oo", "aa"})
if err != nil { if err != nil {
panic(err) panic(err)
return
} }
if a[0] == "xxxx" && a[1] == "oooo" && a[2] == "aaaa" { if a[0] == "xxxx" && a[1] == "oooo" && a[2] == "aaaa" {

View File

@ -39,7 +39,7 @@ func (m *MemoryMapCache[K, V]) Get(_ context.Context, key K) (r V, ok bool) {
return return
} }
r = v.data r = v.data
t := m.expireTime() - time.Now().Sub(v.setTime) t := m.expireTime() - time.Since(v.setTime)
if t <= 0 { if t <= 0 {
ok = false ok = false
} }
@ -68,7 +68,7 @@ func (m *MemoryMapCache[K, V]) Ttl(_ context.Context, key K) time.Duration {
if !ok { if !ok {
return time.Duration(-1) return time.Duration(-1)
} }
return m.expireTime() - time.Now().Sub(v.setTime) return m.expireTime() - time.Since(v.setTime)
} }
func (m *MemoryMapCache[K, V]) Ver(_ context.Context, key K) int { func (m *MemoryMapCache[K, V]) Ver(_ context.Context, key K) int {

View File

@ -15,7 +15,9 @@ var ttt time.Time
func init() { func init() {
ctx = context.Background() ctx = context.Background()
mm = *NewMemoryMapCache[string, string](3 * time.Second) mm = *NewMemoryMapCache[string, string](func() time.Duration {
return 3 * time.Second
})
ttt = time.Now() ttt = time.Now()
mm.Store("aa", mapVal[string]{ mm.Store("aa", mapVal[string]{
setTime: ttt, setTime: ttt,

2
cache/pagination.go vendored
View File

@ -32,7 +32,7 @@ type LocalFn[K comparable, V any] func(ctx context.Context, data []V, k K, page,
func (p *Pagination[K, V]) IsSwitchDB(k K) bool { func (p *Pagination[K, V]) IsSwitchDB(k K) bool {
v, _ := p.isSwitch.Load(k) v, _ := p.isSwitch.Load(k)
return v == true return v
} }
func NewPagination[K comparable, V any](m *MapCache[string, helper.PaginationData[V]], maxNum func() int, func NewPagination[K comparable, V any](m *MapCache[string, helper.PaginationData[V]], maxNum func() int,

2
cache/vars.go vendored
View File

@ -168,7 +168,7 @@ func NewVarMemoryCache[T any](expireTime func() time.Duration) *VarMemoryCache[T
func (c *VarMemoryCache[T]) Get(_ context.Context) (T, bool) { func (c *VarMemoryCache[T]) Get(_ context.Context) (T, bool) {
v := c.v.Load() v := c.v.Load()
return v.data, c.expireTime() >= time.Now().Sub(v.setTime) return v.data, c.expireTime() >= time.Since(v.setTime)
} }
func (c *VarMemoryCache[T]) Set(_ context.Context, v T) { func (c *VarMemoryCache[T]) Set(_ context.Context, v T) {

4
cache/vars_test.go vendored
View File

@ -7,11 +7,11 @@ import (
"time" "time"
) )
var cc = *NewVarCache[int](NewVarMemoryCache[int](func() time.Duration { var cc = *NewVarCache(NewVarMemoryCache[int](func() time.Duration {
return time.Minute return time.Minute
}), func(ctx context.Context, a ...any) (int, error) { }), func(ctx context.Context, a ...any) (int, error) {
return 1, nil return 1, nil
}) }, nil, nil)
func TestVarCache_Flush(t *testing.T) { func TestVarCache_Flush(t *testing.T) {
type testCase[T any] struct { type testCase[T any] struct {

12
go.mod
View File

@ -1,8 +1,8 @@
module github.com/fthvgb1/wp-go module github.com/fthvgb1/wp-go
go 1.23 go 1.23.0
toolchain go1.23.4 toolchain go1.24.2
require ( require (
github.com/dlclark/regexp2 v1.11.4 github.com/dlclark/regexp2 v1.11.4
@ -17,7 +17,7 @@ require (
github.com/go-sql-driver/mysql v1.8.1 github.com/go-sql-driver/mysql v1.8.1
github.com/goccy/go-json v0.10.5 github.com/goccy/go-json v0.10.5
github.com/jmoiron/sqlx v1.4.0 github.com/jmoiron/sqlx v1.4.0
golang.org/x/crypto v0.32.0 golang.org/x/crypto v0.37.0
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
@ -43,9 +43,9 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.13.0 // indirect golang.org/x/arch v0.13.0 // indirect
golang.org/x/net v0.34.0 // indirect golang.org/x/net v0.39.0 // indirect
golang.org/x/sys v0.29.0 // indirect golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.21.0 // indirect golang.org/x/text v0.24.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect google.golang.org/protobuf v1.36.4 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect

View File

@ -81,6 +81,4 @@ It is divided into plug-ins that modify the data of list pages and plug-ins that
The gin framework and sqlx used encapsulate the layer query method outside. The gin framework and sqlx used encapsulate the layer query method outside.
#### Thanks
<a href="https://jb.gg/OpenSourceSupport"><img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png" alt="JetBrains Logo (Main) logo." width="30%"></a>