freelru: fix PurgeExpired

This commit is contained in:
世界 2024-12-03 11:50:46 +08:00
parent 9f69e7f9f7
commit 809d8eca13
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 26 additions and 17 deletions

View file

@ -687,20 +687,19 @@ func (lru *LRU[K, V]) Purge() {
// PurgeExpired purges all expired items from the LRU.
// The evict function is called for each expired item.
func (lru *LRU[K, V]) PurgeExpired() {
n := now()
loop:
l := lru.len
if l == 0 {
return
}
n := now()
pos := lru.head
pos := lru.elements[lru.head].next
for i := uint32(0); i < l; i++ {
next := lru.elements[pos].next
if lru.elements[pos].expire != 0 {
if lru.elements[pos].expire <= n {
lru.removeAt(pos)
}
if lru.elements[pos].expire != 0 && lru.elements[pos].expire <= n {
lru.removeAt(pos)
goto loop
}
pos = next
pos = lru.elements[pos].next
}
}

View file

@ -1,6 +1,9 @@
package freelru_test
import (
"github.com/sagernet/sing/common"
F "github.com/sagernet/sing/common/format"
"math/rand/v2"
"testing"
"time"
@ -75,16 +78,23 @@ func TestUpdateLifetime2(t *testing.T) {
require.False(t, ok)
}
func TestPeekWithLifetime(t *testing.T) {
func TestPurgeExpired(t *testing.T) {
t.Parallel()
lru, err := freelru.New[string, string](1024, maphash.NewHasher[string]().Hash32)
lru, err := freelru.New[string, *string](1024, maphash.NewHasher[string]().Hash32)
require.NoError(t, err)
lru.SetLifetime(time.Second)
lru.AddWithLifetime("hello", "world", 10*time.Second)
lru.Add("hello1", "")
lru.Add("hello2", "")
lru.Add("hello3", "")
time.Sleep(2 * time.Second)
lru.PurgeExpired()
require.Equal(t, 1, lru.Len())
lru.SetOnEvict(func(s string, s2 *string) {
if s2 == nil {
t.Fail()
}
})
for i := 0; i < 100; i++ {
lru.AddWithLifetime("hello_"+F.ToString(i), common.Ptr("world_"+F.ToString(i)), time.Duration(rand.Int32N(3000))*time.Millisecond)
}
for i := 0; i < 5; i++ {
time.Sleep(time.Second)
lru.GetAndRefreshOrAdd("hellox"+F.ToString(i), func() (*string, bool) {
return common.Ptr("worldx"), true
})
}
}