From: qydysky Date: Thu, 25 May 2023 17:42:04 +0000 (+0800) Subject: add X-Git-Tag: v0.27.13~2 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=323ef9010e937c312f3066b6c500e52adfad22c6;p=part%2F.git add --- diff --git a/sync/Map.go b/sync/Map.go index 67c2ebb..b74cef1 100644 --- a/sync/Map.go +++ b/sync/Map.go @@ -99,7 +99,11 @@ func (t *MapExceeded[K, V]) Range(f func(key K, value *V) bool) { }) } -func (t *MapExceeded[K, V]) GC(dur ...time.Duration) { +func (t *MapExceeded[K, V]) Len() int { + return t.m.Len() +} + +func (t *MapExceeded[K, V]) GC() { t.m.Range(func(key, value any) bool { if value.(mapExceededItem[V]).exceeded.Before(time.Now()) { t.Delete(key.(K)) diff --git a/web/Web.go b/web/Web.go index ec17ac8..919d3a0 100644 --- a/web/Web.go +++ b/web/Web.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" psync "github.com/qydysky/part/sync" @@ -224,7 +225,8 @@ func (t *CountLimits) AddCount(r *http.Request) (isOverflow bool) { } type Cache struct { - g psync.MapExceeded[string, []byte] + g psync.MapExceeded[string, []byte] + gcL atomic.Int64 } func (t *Cache) IsCache(key string) (res *[]byte, isCache bool) { @@ -233,6 +235,10 @@ func (t *Cache) IsCache(key string) (res *[]byte, isCache bool) { func (t *Cache) Store(key string, aliveDur time.Duration, data []byte) { t.g.Store(key, data, aliveDur) + if s := int64(t.g.Len()); s > 10 && t.gcL.Load() <= s { + t.gcL.Store(s * 2) + t.g.GC() + } } func Easy_boot() *Web {