From c96213ae1ad1af4552bb7ed69fda0345b9af9f54 Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 20 Apr 2025 13:53:23 +0800 Subject: [PATCH] optimize code, fix bug and upgrade unsafe package --- README.md | 2 -- app/middleware/iplimit.go | 2 +- cache/map.go | 12 ++++-------- cache/map_test.go | 8 ++++---- cache/memorymapcache.go | 4 ++-- cache/memorymapcache_test.go | 4 +++- cache/pagination.go | 2 +- cache/vars.go | 2 +- cache/vars_test.go | 4 ++-- go.mod | 12 ++++++------ readme_en.md | 2 -- 11 files changed, 24 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 60c0ba7..fc5e384 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,5 @@ go run app/cmd/main.go [-c configpath] [-p port] 用的gin框架和sqlx,在外面封装了层查询的方法。 -#### 鸣谢 -JetBrains Logo (Main) logo. diff --git a/app/middleware/iplimit.go b/app/middleware/iplimit.go index 5f85790..512df71 100755 --- a/app/middleware/iplimit.go +++ b/app/middleware/iplimit.go @@ -155,7 +155,7 @@ func IpLimit(num int64, clearNum ...int64) (func(ctx *gin.Context), func(int64, a := NewFlowLimits(func(c *gin.Context) string { return c.ClientIP() }, ToManyRequest(), IpLimitClear) - return CustomFlowLimit[string](a, num, clearNum...) + return CustomFlowLimit(a, num, clearNum...) } const minute = "2006-01-02 15:04" diff --git a/cache/map.go b/cache/map.go index d74eaf1..71191fd 100644 --- a/cache/map.go +++ b/cache/map.go @@ -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) res = mm - var flushKeys []K for i, k := range key { _, ok := mm[k] if !ok { - flushKeys = append(flushKeys, k) needIndex[k] = i } } @@ -354,7 +352,7 @@ func (m *MapCache[K, V]) getBatchToMap(e Expend[K, V]) func(c context.Context, k }() select { 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 case <-done: } @@ -421,7 +419,7 @@ func (m *MapCache[K, V]) getBatchToMapes(c context.Context, key []K, timeout tim }() select { 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 case <-done: } @@ -487,7 +485,7 @@ func (m *MapCache[K, V]) getCacheBatchs(c context.Context, key []K, timeout time }() select { 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 case <-done: } @@ -508,11 +506,9 @@ func (m *MapCache[K, V]) getBatches(e Expend[K, V]) func(ctx context.Context, ke if err != nil { return nil, err } - var flushKeys []K for i, k := range key { v, ok := mm[k] if !ok { - flushKeys = append(flushKeys, k) needIndex[k] = i var vv V v = vv @@ -568,7 +564,7 @@ func (m *MapCache[K, V]) getBatches(e Expend[K, V]) func(ctx context.Context, ke }() select { 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 case <-done: } diff --git a/cache/map_test.go b/cache/map_test.go index de63aa6..a33900b 100644 --- a/cache/map_test.go +++ b/cache/map_test.go @@ -3,12 +3,13 @@ package cache import ( "context" "fmt" - "github.com/fthvgb1/wp-go/helper/slice" - "github.com/fthvgb1/wp-go/taskPools" "reflect" "strings" "testing" "time" + + "github.com/fthvgb1/wp-go/helper/slice" + "github.com/fthvgb1/wp-go/taskPools" ) var ca MapCache[string, string] @@ -67,7 +68,7 @@ func TestMapCache_Flush(t *testing.T) { m MapCache[K, V] args args } - ca := *NewMapCache[string, string](NewMemoryMapCache[string, string](func() time.Duration { + ca := *NewMapCache(NewMemoryMapCache[string, string](func() time.Duration { return time.Second }), fn, nil, nil, nil) _, _ = 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"}) if err != nil { panic(err) - return } if a[0] == "xxxx" && a[1] == "oooo" && a[2] == "aaaa" { diff --git a/cache/memorymapcache.go b/cache/memorymapcache.go index 01a31f7..31c0123 100644 --- a/cache/memorymapcache.go +++ b/cache/memorymapcache.go @@ -39,7 +39,7 @@ func (m *MemoryMapCache[K, V]) Get(_ context.Context, key K) (r V, ok bool) { return } r = v.data - t := m.expireTime() - time.Now().Sub(v.setTime) + t := m.expireTime() - time.Since(v.setTime) if t <= 0 { ok = false } @@ -68,7 +68,7 @@ func (m *MemoryMapCache[K, V]) Ttl(_ context.Context, key K) time.Duration { if !ok { 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 { diff --git a/cache/memorymapcache_test.go b/cache/memorymapcache_test.go index 51982f3..f07297f 100644 --- a/cache/memorymapcache_test.go +++ b/cache/memorymapcache_test.go @@ -15,7 +15,9 @@ var ttt time.Time func init() { ctx = context.Background() - mm = *NewMemoryMapCache[string, string](3 * time.Second) + mm = *NewMemoryMapCache[string, string](func() time.Duration { + return 3 * time.Second + }) ttt = time.Now() mm.Store("aa", mapVal[string]{ setTime: ttt, diff --git a/cache/pagination.go b/cache/pagination.go index d1be523..ce1f569 100644 --- a/cache/pagination.go +++ b/cache/pagination.go @@ -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 { 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, diff --git a/cache/vars.go b/cache/vars.go index 39ca2d4..a421235 100644 --- a/cache/vars.go +++ b/cache/vars.go @@ -168,7 +168,7 @@ func NewVarMemoryCache[T any](expireTime func() time.Duration) *VarMemoryCache[T func (c *VarMemoryCache[T]) Get(_ context.Context) (T, bool) { 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) { diff --git a/cache/vars_test.go b/cache/vars_test.go index 6ea26ff..b2565cd 100644 --- a/cache/vars_test.go +++ b/cache/vars_test.go @@ -7,11 +7,11 @@ import ( "time" ) -var cc = *NewVarCache[int](NewVarMemoryCache[int](func() time.Duration { +var cc = *NewVarCache(NewVarMemoryCache[int](func() time.Duration { return time.Minute }), func(ctx context.Context, a ...any) (int, error) { return 1, nil -}) +}, nil, nil) func TestVarCache_Flush(t *testing.T) { type testCase[T any] struct { diff --git a/go.mod b/go.mod index 977c5ce..d3f3007 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,8 @@ module github.com/fthvgb1/wp-go -go 1.23 +go 1.23.0 -toolchain go1.23.4 +toolchain go1.24.2 require ( github.com/dlclark/regexp2 v1.11.4 @@ -17,7 +17,7 @@ require ( github.com/go-sql-driver/mysql v1.8.1 github.com/goccy/go-json v0.10.5 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 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/yaml.v2 v2.4.0 @@ -43,9 +43,9 @@ require ( github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect golang.org/x/arch v0.13.0 // indirect - golang.org/x/net v0.34.0 // indirect - golang.org/x/sys v0.29.0 // indirect - golang.org/x/text v0.21.0 // indirect + golang.org/x/net v0.39.0 // indirect + golang.org/x/sys v0.32.0 // indirect + golang.org/x/text v0.24.0 // indirect google.golang.org/protobuf v1.36.4 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/readme_en.md b/readme_en.md index aa28710..59c1415 100644 --- a/readme_en.md +++ b/readme_en.md @@ -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. -#### Thanks -JetBrains Logo (Main) logo.