From a42bd4a9c81be8cc46a3a69ed6daee51c3afa551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 13 Aug 2020 16:10:22 +0200 Subject: [PATCH] Use typed *Item in DeleteFunc --- bucket.go | 4 ++-- cache.go | 2 +- cache_test.go | 8 ++++---- layeredbucket.go | 2 +- layeredcache.go | 2 +- layeredcache_test.go | 8 ++++---- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/bucket.go b/bucket.go index cf896c6..e194436 100644 --- a/bucket.go +++ b/bucket.go @@ -54,7 +54,7 @@ func (b *bucket) delete(key string) *Item { // the item from the map. I'm pretty sure this is 100% fine, but it is unique. // (We do this so that the write to the channel is under the read lock and not the // write lock) -func (b *bucket) deleteFunc(matches func(key string, item interface{}) bool, deletables chan *Item) int { +func (b *bucket) deleteFunc(matches func(key string, item *Item) bool, deletables chan *Item) int { lookup := b.lookup items := make([]*Item, 0) @@ -81,7 +81,7 @@ func (b *bucket) deleteFunc(matches func(key string, item interface{}) bool, del } func (b *bucket) deletePrefix(prefix string, deletables chan *Item) int { - return b.deleteFunc(func(key string, item interface{}) bool { + return b.deleteFunc(func(key string, item *Item) bool { return strings.HasPrefix(key, prefix) }, deletables) } diff --git a/cache.go b/cache.go index e9d1924..8649f9c 100644 --- a/cache.go +++ b/cache.go @@ -64,7 +64,7 @@ func (c *Cache) DeletePrefix(prefix string) int { } // Deletes all items that the matches func evaluates to true. -func (c *Cache) DeleteFunc(matches func(key string, item interface{}) bool) int { +func (c *Cache) DeleteFunc(matches func(key string, item *Item) bool) int { count := 0 for _, b := range c.buckets { count += b.deleteFunc(matches, c.deletables) diff --git a/cache_test.go b/cache_test.go index e128240..e1ac022 100644 --- a/cache_test.go +++ b/cache_test.go @@ -63,17 +63,17 @@ func (_ CacheTests) DeletesAFunc() { cache.Set("f", 6, time.Minute) Expect(cache.ItemCount()).To.Equal(6) - Expect(cache.DeleteFunc(func(key string, item interface{}) bool { + Expect(cache.DeleteFunc(func(key string, item *Item) bool { return false })).To.Equal(0) Expect(cache.ItemCount()).To.Equal(6) - Expect(cache.DeleteFunc(func(key string, item interface{}) bool { - return item.(*Item).Value().(int) < 4 + Expect(cache.DeleteFunc(func(key string, item *Item) bool { + return item.Value().(int) < 4 })).To.Equal(3) Expect(cache.ItemCount()).To.Equal(3) - Expect(cache.DeleteFunc(func(key string, item interface{}) bool { + Expect(cache.DeleteFunc(func(key string, item *Item) bool { return key == "d" })).To.Equal(1) Expect(cache.ItemCount()).To.Equal(2) diff --git a/layeredbucket.go b/layeredbucket.go index a0514e0..4e57211 100644 --- a/layeredbucket.go +++ b/layeredbucket.go @@ -71,7 +71,7 @@ func (b *layeredBucket) deletePrefix(primary, prefix string, deletables chan *It return bucket.deletePrefix(prefix, deletables) } -func (b *layeredBucket) deleteFunc(primary string, matches func(key string, item interface{}) bool, deletables chan *Item) int { +func (b *layeredBucket) deleteFunc(primary string, matches func(key string, item *Item) bool, deletables chan *Item) int { b.RLock() bucket, exists := b.buckets[primary] b.RUnlock() diff --git a/layeredcache.go b/layeredcache.go index 6786880..e95977f 100644 --- a/layeredcache.go +++ b/layeredcache.go @@ -155,7 +155,7 @@ func (c *LayeredCache) DeletePrefix(primary, prefix string) int { } // Deletes all items that share the same primary key and where the matches func evaluates to true. -func (c *LayeredCache) DeleteFunc(primary string, matches func(key string, item interface{}) bool) int { +func (c *LayeredCache) DeleteFunc(primary string, matches func(key string, item *Item) bool) int { return c.bucket(primary).deleteFunc(primary, matches, c.deletables) } diff --git a/layeredcache_test.go b/layeredcache_test.go index d0edeb2..6ca33ce 100644 --- a/layeredcache_test.go +++ b/layeredcache_test.go @@ -107,17 +107,17 @@ func (_ *LayeredCacheTests) DeletesAFunc() { cache.Set("spice", "f", 6, time.Minute) Expect(cache.ItemCount()).To.Equal(6) - Expect(cache.DeleteFunc("spice", func(key string, item interface{}) bool { + Expect(cache.DeleteFunc("spice", func(key string, item *Item) bool { return false })).To.Equal(0) Expect(cache.ItemCount()).To.Equal(6) - Expect(cache.DeleteFunc("spice", func(key string, item interface{}) bool { - return item.(*Item).Value().(int) < 4 + Expect(cache.DeleteFunc("spice", func(key string, item *Item) bool { + return item.Value().(int) < 4 })).To.Equal(2) Expect(cache.ItemCount()).To.Equal(4) - Expect(cache.DeleteFunc("spice", func(key string, item interface{}) bool { + Expect(cache.DeleteFunc("spice", func(key string, item *Item) bool { return key == "d" })).To.Equal(1) Expect(cache.ItemCount()).To.Equal(3)