From e6e2171aa91e617f0fbe45ed7348aa7f9ca11600 Mon Sep 17 00:00:00 2001 From: Eero Norri Date: Fri, 9 Jun 2023 09:59:00 +0300 Subject: [PATCH 1/4] Add Len() to common interface - implement Len for simple cache --- cache.go | 9 +++++++++ example_test.go | 10 ++++++++++ policy/simple/simple.go | 5 +++++ 3 files changed, 24 insertions(+) diff --git a/cache.go b/cache.go index df75728..54206fd 100644 --- a/cache.go +++ b/cache.go @@ -23,6 +23,8 @@ type Interface[K comparable, V any] interface { Keys() []K // Delete deletes the item with provided key from the cache. Delete(key K) + // Len returns the number of items in the cache. + Len() int } var ( @@ -253,6 +255,13 @@ func (c *Cache[K, V]) Delete(key K) { c.cache.Delete(key) } +// Len returns the number of items in the cache. +func (c *Cache[K, V]) Len() int { + c.mu.Lock() + defer c.mu.Unlock() + return c.cache.Len() +} + // Contains reports whether key is within cache. func (c *Cache[K, V]) Contains(key K) bool { c.mu.Lock() diff --git a/example_test.go b/example_test.go index 97ddbe0..7e2ae3e 100644 --- a/example_test.go +++ b/example_test.go @@ -123,6 +123,16 @@ func ExampleCache_Keys() { // [a b c] } +func ExampleCache_Len() { + c := cache.New(cache.AsLFU[string, int]()) + c.Set("a", 1) + c.Set("b", 1) + c.Set("c", 1) + fmt.Println(c.Len()) + // Output: + // 3 +} + func ExampleCache_Contains() { c := cache.New(cache.AsLRU[string, int]()) c.Set("a", 1) diff --git a/policy/simple/simple.go b/policy/simple/simple.go index 2ed4cdb..cb15ccd 100644 --- a/policy/simple/simple.go +++ b/policy/simple/simple.go @@ -59,3 +59,8 @@ func (c *Cache[K, _]) Keys() []K { func (c *Cache[K, V]) Delete(key K) { delete(c.items, key) } + +// Len returns the number of items in the cache. +func (c *Cache[K, V]) Len() int { + return len(c.items) +} From 9c6b76ae1fd64ac3267584ebc511dd1c4f61b83c Mon Sep 17 00:00:00 2001 From: Eero Norri Date: Mon, 12 Jun 2023 19:46:13 +0300 Subject: [PATCH 2/4] Change common interface to use Keys() in Len() method --- cache.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cache.go b/cache.go index 54206fd..58bf54d 100644 --- a/cache.go +++ b/cache.go @@ -257,9 +257,7 @@ func (c *Cache[K, V]) Delete(key K) { // Len returns the number of items in the cache. func (c *Cache[K, V]) Len() int { - c.mu.Lock() - defer c.mu.Unlock() - return c.cache.Len() + return len(c.Keys()) } // Contains reports whether key is within cache. From f08595aaf3c4c77885f98d964c5ab089f3c7b2c7 Mon Sep 17 00:00:00 2001 From: Eero Norri Date: Sun, 6 Aug 2023 14:31:26 +0300 Subject: [PATCH 3/4] Add benchmark tests for Len() in simple policy --- policy/simple/example_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/policy/simple/example_test.go b/policy/simple/example_test.go index bfe9c28..37f6a6a 100644 --- a/policy/simple/example_test.go +++ b/policy/simple/example_test.go @@ -2,6 +2,7 @@ package simple_test import ( "fmt" + "testing" "github.com/Code-Hex/go-generics-cache/policy/simple" ) @@ -47,3 +48,23 @@ func ExampleCache_Keys() { // bar // baz } + +func BenchmarkLenWithKeys(b *testing.B) { + c := simple.NewCache[string, int]() + c.Set("foo", 1) + c.Set("bar", 2) + c.Set("baz", 3) + for i := 0; i < b.N; i++ { + var _ = len(c.Keys()) + } +} + +func BenchmarkJustLen(b *testing.B) { + c := simple.NewCache[string, int]() + c.Set("foo", 1) + c.Set("bar", 2) + c.Set("baz", 3) + for i := 0; i < b.N; i++ { + var _ = c.Len() + } +} From 3621c3330786f5a5db5f13d73f83beccf5cf75cb Mon Sep 17 00:00:00 2001 From: Eero Norri Date: Mon, 7 Aug 2023 12:47:41 +0300 Subject: [PATCH 4/4] Change cache interface to use internal Len method --- cache.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cache.go b/cache.go index 58bf54d..54206fd 100644 --- a/cache.go +++ b/cache.go @@ -257,7 +257,9 @@ func (c *Cache[K, V]) Delete(key K) { // Len returns the number of items in the cache. func (c *Cache[K, V]) Len() int { - return len(c.Keys()) + c.mu.Lock() + defer c.mu.Unlock() + return c.cache.Len() } // Contains reports whether key is within cache.