From: qydysky Date: Wed, 1 May 2024 06:39:46 +0000 (+0000) Subject: 1 X-Git-Tag: v0.28.20240501064531 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=b8bc3e2294d4b7718f452dce79f546fd38fa8d30;p=part%2F.git 1 --- diff --git a/sync/Map.go b/sync/Map.go index 18ef018..abc0589 100644 --- a/sync/Map.go +++ b/sync/Map.go @@ -84,6 +84,17 @@ type mapExceededItem[V any] struct { wait sync.RWMutex } +func (t *MapExceeded[K, V]) Copy() (m *MapExceeded[K, V]) { + m = &MapExceeded[K, V]{} + t.m.Range(func(key, value any) bool { + if value.(*mapExceededItem[V]).exceeded.After(time.Now()) { + m.m.Store(key, value) + } + return true + }) + return +} + func (t *MapExceeded[K, V]) Store(k K, v *V, dur time.Duration) { t.m.Store(k, &mapExceededItem[V]{ data: v, diff --git a/sync/Map_test.go b/sync/Map_test.go index ef821ec..5754273 100644 --- a/sync/Map_test.go +++ b/sync/Map_test.go @@ -373,3 +373,22 @@ func TestMapExceeded2(t *testing.T) { } w.Wait() } + +func TestMapExceeded3(t *testing.T) { + var m MapExceeded[string, []byte] + var data = []byte("1") + if v, loaded, f := m.LoadOrStore("1"); v != nil || loaded { + t.Fatal() + } else { + f(&data, time.Second) + if v, ok := m.Load("1"); !ok || v == nil || !bytes.Equal(data, *v) { + t.Fatal() + } + } + + p := m.Copy() + + if v, ok := p.Load("1"); !ok || v == nil || !bytes.Equal(data, *v) { + t.Fatal() + } +}