1 Commits

Author SHA1 Message Date
b92e16fa4d Handle evictions properly when cache is empty. 2023-08-27 18:01:16 -07:00
4 changed files with 12 additions and 1 deletions

2
go.mod
View File

@@ -2,4 +2,4 @@ module git.wntrmute.dev/kyle/go-mru
go 1.17
require github.com/benbjohnson/clock v1.3.0
require github.com/benbjohnson/clock v1.3.5

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=

4
mru.go
View File

@@ -53,6 +53,10 @@ func (c *Cache) Len() int {
// evict should remove the least-recently-used cache item.
func (c *Cache) evict() {
if c.access.Len() == 0 {
return
}
k := c.access.K(0)
c.evictKey(k)
}

View File

@@ -20,6 +20,11 @@ func TestBasicCacheEviction(t *testing.T) {
t.Fatal("cache should have size 0")
}
c.evict()
if err := c.ConsistencyCheck(); err != nil {
t.Fatal(err)
}
c.Store("raven", 1)
if err := c.ConsistencyCheck(); err != nil {
t.Fatal(err)