From c39efbc76bd396bf9e4d2db624e3772a731afe91 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Thu, 18 Nov 2021 18:25:18 -0500 Subject: [PATCH] Return an error instead of panic --- cache.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/cache.go b/cache.go index 308a6c5..af409a3 100644 --- a/cache.go +++ b/cache.go @@ -400,10 +400,10 @@ func (cache *Cache) Stats() Stats { } // Resize the cache to hold at most n entries. If n is smaller than the current -// size, entries are evicted to fit the new size. -func (cache *Cache) Resize(n int) { +// size, entries are evicted to fit the new size. It errors if n <= 0. +func (cache *Cache) Resize(n int) error { if n <= 0 { - panic("Must supply a positive capacity to Resize") + return errors.New("must supply a positive capacity to Resize") } cache.mutex.Lock() @@ -411,15 +411,14 @@ func (cache *Cache) Resize(n int) { c := cache.capacity cache.capacity = n - if n >= c { - return - } - for i := 0; i < c - n; i++ { + for i := 0; i < c-n; i++ { successful := cache.evictOldest() if !successful { break } } + + return nil } func (cache *Cache) deleteExpired() {