]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240501130702
authorqydysky <qydysky@foxmail.com>
Wed, 1 May 2024 13:01:37 +0000 (13:01 +0000)
committerqydysky <qydysky@foxmail.com>
Wed, 1 May 2024 13:01:37 +0000 (13:01 +0000)
sync/Map.go
sync/Map_test.go
web/Web.go

index abc0589fbd7f8685f1caa8022ab4e5609c6714a5..6eb3de1a9d3fcdd0cfd13966abdeb8a82a59dc59 100644 (file)
@@ -79,7 +79,7 @@ type MapExceeded[K, V any] struct {
 }
 
 type mapExceededItem[V any] struct {
-       data     *V
+       data     V
        exceeded time.Time
        wait     sync.RWMutex
 }
@@ -95,21 +95,21 @@ func (t *MapExceeded[K, V]) Copy() (m *MapExceeded[K, V]) {
        return
 }
 
-func (t *MapExceeded[K, V]) Store(k K, v *V, dur time.Duration) {
+func (t *MapExceeded[K, V]) Store(k K, v V, dur time.Duration) {
        t.m.Store(k, &mapExceededItem[V]{
                data:     v,
                exceeded: time.Now().Add(dur),
        })
 }
 
-func (t *MapExceeded[K, V]) Load(k K) (*V, bool) {
+func (t *MapExceeded[K, V]) Load(k K) (v V, ok bool) {
        if v, ok := t.m.LoadV(k).(*mapExceededItem[V]); ok {
                if v.exceeded.After(time.Now()) {
                        return v.data, true
                }
                t.Delete(k)
        }
-       return nil, false
+       return
 }
 
 func (t *MapExceeded[K, V]) Range(f func(key K, value *V) bool) {
@@ -139,8 +139,8 @@ func (t *MapExceeded[K, V]) Delete(k K) {
        t.m.Delete(k)
 }
 
-func (t *MapExceeded[K, V]) LoadOrStore(k K) (vr *V, loaded bool, store func(v1 *V, dur time.Duration)) {
-       store = func(v1 *V, dur time.Duration) {}
+func (t *MapExceeded[K, V]) LoadOrStore(k K) (vr V, loaded bool, store func(v1 V, dur time.Duration)) {
+       store = func(v1 V, dur time.Duration) {}
        var actual any
        actual, loaded = t.m.LoadOrStore(k, &mapExceededItem[V]{})
        v := actual.(*mapExceededItem[V])
@@ -152,7 +152,7 @@ func (t *MapExceeded[K, V]) LoadOrStore(k K) (vr *V, loaded bool, store func(v1
                return
        }
        if !loaded || (loaded && !exp.IsZero()) {
-               store = func(v1 *V, dur time.Duration) {
+               store = func(v1 V, dur time.Duration) {
                        v.wait.Lock()
                        v.data = v1
                        v.exceeded = time.Now().Add(dur)
index 57542735cff7c772000ef01ef29966e3e73526e2..7b097253f37a22eac14d0313b85c7d8fca758890 100644 (file)
@@ -335,7 +335,7 @@ func Benchmark_syncMap_Range(b *testing.B) {
 }
 
 func TestMapExceeded1(t *testing.T) {
-       var m MapExceeded[string, []byte]
+       var m MapExceeded[string, *[]byte]
        var data = []byte("1")
        m.Store("1", &data, time.Second)
        if b, ok := m.Load("1"); !ok || 0 != bytes.Compare(*b, []byte("1")) {
@@ -348,7 +348,7 @@ func TestMapExceeded1(t *testing.T) {
 }
 
 func TestMapExceeded2(t *testing.T) {
-       var m MapExceeded[string, []byte]
+       var m MapExceeded[string, *[]byte]
        var data = []byte("1")
        if v, loaded, f := m.LoadOrStore("1"); v != nil || loaded {
                t.Fatal()
@@ -375,7 +375,7 @@ func TestMapExceeded2(t *testing.T) {
 }
 
 func TestMapExceeded3(t *testing.T) {
-       var m MapExceeded[string, []byte]
+       var m MapExceeded[string, *[]byte]
        var data = []byte("1")
        if v, loaded, f := m.LoadOrStore("1"); v != nil || loaded {
                t.Fatal()
index 47d04641e12bb38eab3a34ea887778108280732c..1f5f8e2e91a1be2d6a0f5b2f0fd9fa2ebc36b901 100644 (file)
@@ -485,7 +485,7 @@ func (t *limitItem) Request(matchf func(req *http.Request) (match bool)) *limitI
 }
 
 type Cache struct {
-       g   psync.MapExceeded[string, []byte]
+       g   psync.MapExceeded[string, *[]byte]
        gcL atomic.Int64
 }