Skip to content

Commit

Permalink
Return an error instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletier committed Nov 18, 2021
1 parent bab267a commit c39efbc
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,26 +400,25 @@ 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()
defer cache.mutex.Unlock()
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() {
Expand Down

0 comments on commit c39efbc

Please sign in to comment.