From f2e3b297d109c71e21be7fa57becafa809bf2171 Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Mon, 7 Aug 2023 17:18:39 +0200 Subject: [PATCH] rename NewExpirableLRU to NewLRU --- README.md | 2 +- expirable/expirable_lru.go | 4 ++-- expirable/expirable_lru_test.go | 30 +++++++++++++++--------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2034094..922bc7d 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ import ( func main() { // make cache with 10ms TTL and 5 max keys - cache := expirable.NewExpirableLRU[string, string](5, nil, time.Millisecond*10) + cache := expirable.NewLRU[string, string](5, nil, time.Millisecond*10) // expirable cache need to be closed after used defer cache.Close() diff --git a/expirable/expirable_lru.go b/expirable/expirable_lru.go index 997fdf5..6b9221f 100644 --- a/expirable/expirable_lru.go +++ b/expirable/expirable_lru.go @@ -41,14 +41,14 @@ const noEvictionTTL = time.Hour * 24 * 365 * 10 // casting it as uint8 explicitly requires type conversions in multiple places const numBuckets = 100 -// NewExpirableLRU returns a new thread-safe cache with expirable entries. +// NewLRU returns a new thread-safe cache with expirable entries. // // Size parameter set to 0 makes cache of unlimited size, e.g. turns LRU mechanism off. // // Providing 0 TTL turns expiring off. // // Delete expired entries every 1/100th of ttl value. -func NewExpirableLRU[K comparable, V any](size int, onEvict EvictCallback[K, V], ttl time.Duration) *LRU[K, V] { +func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V], ttl time.Duration) *LRU[K, V] { if size < 0 { size = 0 } diff --git a/expirable/expirable_lru_test.go b/expirable/expirable_lru_test.go index a2aa67a..58529d7 100644 --- a/expirable/expirable_lru_test.go +++ b/expirable/expirable_lru_test.go @@ -14,7 +14,7 @@ import ( ) func BenchmarkLRU_Rand_NoExpire(b *testing.B) { - l := NewExpirableLRU[int64, int64](8192, nil, 0) + l := NewLRU[int64, int64](8192, nil, 0) trace := make([]int64, b.N*2) for i := 0; i < b.N*2; i++ { @@ -39,7 +39,7 @@ func BenchmarkLRU_Rand_NoExpire(b *testing.B) { } func BenchmarkLRU_Freq_NoExpire(b *testing.B) { - l := NewExpirableLRU[int64, int64](8192, nil, 0) + l := NewLRU[int64, int64](8192, nil, 0) trace := make([]int64, b.N*2) for i := 0; i < b.N*2; i++ { @@ -67,7 +67,7 @@ func BenchmarkLRU_Freq_NoExpire(b *testing.B) { } func BenchmarkLRU_Rand_WithExpire(b *testing.B) { - l := NewExpirableLRU[int64, int64](8192, nil, time.Millisecond*10) + l := NewLRU[int64, int64](8192, nil, time.Millisecond*10) defer l.Close() trace := make([]int64, b.N*2) @@ -93,7 +93,7 @@ func BenchmarkLRU_Rand_WithExpire(b *testing.B) { } func BenchmarkLRU_Freq_WithExpire(b *testing.B) { - l := NewExpirableLRU[int64, int64](8192, nil, time.Millisecond*10) + l := NewLRU[int64, int64](8192, nil, time.Millisecond*10) defer l.Close() trace := make([]int64, b.N*2) @@ -126,7 +126,7 @@ func TestLRUInterface(_ *testing.T) { } func TestLRUNoPurge(t *testing.T) { - lc := NewExpirableLRU[string, string](10, nil, 0) + lc := NewLRU[string, string](10, nil, 0) lc.Add("key1", "val1") if lc.Len() != 1 { @@ -173,7 +173,7 @@ func TestLRUNoPurge(t *testing.T) { } func TestLRUEdgeCases(t *testing.T) { - lc := NewExpirableLRU[string, *string](2, nil, 0) + lc := NewLRU[string, *string](2, nil, 0) // Adding a nil value lc.Add("key1", nil) @@ -194,7 +194,7 @@ func TestLRUEdgeCases(t *testing.T) { } func TestLRU_Values(t *testing.T) { - lc := NewExpirableLRU[string, string](3, nil, 0) + lc := NewLRU[string, string](3, nil, 0) defer lc.Close() lc.Add("key1", "val1") @@ -208,7 +208,7 @@ func TestLRU_Values(t *testing.T) { } func TestExpirableMultipleClose(_ *testing.T) { - lc := NewExpirableLRU[string, string](10, nil, 0) + lc := NewLRU[string, string](10, nil, 0) lc.Close() // should not panic lc.Close() @@ -216,7 +216,7 @@ func TestExpirableMultipleClose(_ *testing.T) { func TestLRUWithPurge(t *testing.T) { var evicted []string - lc := NewExpirableLRU(10, func(key string, value string) { evicted = append(evicted, key, value) }, 150*time.Millisecond) + lc := NewLRU(10, func(key string, value string) { evicted = append(evicted, key, value) }, 150*time.Millisecond) defer lc.Close() k, v, ok := lc.GetOldest() @@ -298,7 +298,7 @@ func TestLRUWithPurge(t *testing.T) { } func TestLRUWithPurgeEnforcedBySize(t *testing.T) { - lc := NewExpirableLRU[string, string](10, nil, time.Hour) + lc := NewLRU[string, string](10, nil, time.Hour) defer lc.Close() for i := 0; i < 100; i++ { @@ -322,7 +322,7 @@ func TestLRUWithPurgeEnforcedBySize(t *testing.T) { } func TestLRUConcurrency(t *testing.T) { - lc := NewExpirableLRU[string, string](0, nil, 0) + lc := NewLRU[string, string](0, nil, 0) wg := sync.WaitGroup{} wg.Add(1000) for i := 0; i < 1000; i++ { @@ -339,7 +339,7 @@ func TestLRUConcurrency(t *testing.T) { func TestLRUInvalidateAndEvict(t *testing.T) { var evicted int - lc := NewExpirableLRU(-1, func(_, _ string) { evicted++ }, 0) + lc := NewLRU(-1, func(_, _ string) { evicted++ }, 0) lc.Add("key1", "val1") lc.Add("key2", "val2") @@ -369,7 +369,7 @@ func TestLRUInvalidateAndEvict(t *testing.T) { } func TestLoadingExpired(t *testing.T) { - lc := NewExpirableLRU[string, string](0, nil, time.Millisecond*5) + lc := NewLRU[string, string](0, nil, time.Millisecond*5) defer lc.Close() lc.Add("key1", "val1") @@ -416,7 +416,7 @@ func TestLoadingExpired(t *testing.T) { } func TestLRURemoveOldest(t *testing.T) { - lc := NewExpirableLRU[string, string](2, nil, 0) + lc := NewLRU[string, string](2, nil, 0) k, v, ok := lc.RemoveOldest() if k != "" { @@ -483,7 +483,7 @@ func TestLRURemoveOldest(t *testing.T) { func ExampleLRU() { // make cache with 10ms TTL and 5 max keys - cache := NewExpirableLRU[string, string](5, nil, time.Millisecond*10) + cache := NewLRU[string, string](5, nil, time.Millisecond*10) // expirable cache need to be closed after used defer cache.Close()