Skip to content

Commit

Permalink
rename NewExpirableLRU to NewLRU
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Aug 7, 2023
1 parent 1154eab commit f2e3b29
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions expirable/expirable_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 15 additions & 15 deletions expirable/expirable_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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++ {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -208,15 +208,15 @@ 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()
}

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()
Expand Down Expand Up @@ -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++ {
Expand All @@ -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++ {
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit f2e3b29

Please sign in to comment.