})
}
-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))
"strconv"
"strings"
"sync"
+ "sync/atomic"
"time"
psync "github.com/qydysky/part/sync"
}
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) {
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 {