update to golang-lru v2

Michael Muré created

Change summary

cache/lru_id_cache.go    | 43 ++++++++---------------------------------
cache/repo_cache_test.go |  2 
cache/subcache.go        |  8 +++---
go.mod                   |  3 +
go.sum                   |  5 ++-
5 files changed, 19 insertions(+), 42 deletions(-)

Detailed changes

cache/lru_id_cache.go 🔗

@@ -3,54 +3,29 @@ package cache
 import (
 	"math"
 
-	lru "github.com/hashicorp/golang-lru"
+	lru "github.com/hashicorp/golang-lru/v2"
 
 	"github.com/MichaelMure/git-bug/entity"
 )
 
 type lruIdCache struct {
-	lru *lru.Cache
+	*lru.Cache[entity.Id, *struct{}]
 }
 
-func newLRUIdCache() *lruIdCache {
+func newLRUIdCache() lruIdCache {
 	// we can ignore the error here as it would only fail if the size is negative.
-	cache, _ := lru.New(math.MaxInt32)
-
-	return &lruIdCache{
-		cache,
-	}
+	cache, _ := lru.New[entity.Id, *struct{}](math.MaxInt32)
+	return lruIdCache{Cache: cache}
 }
 
 func (c *lruIdCache) Add(id entity.Id) bool {
-	return c.lru.Add(id, nil)
-}
-
-func (c *lruIdCache) Contains(id entity.Id) bool {
-	return c.lru.Contains(id)
-}
-
-func (c *lruIdCache) Get(id entity.Id) bool {
-	_, present := c.lru.Get(id)
-	return present
+	return c.Cache.Add(id, nil)
 }
-
 func (c *lruIdCache) GetOldest() (entity.Id, bool) {
-	id, _, present := c.lru.GetOldest()
-	return id.(entity.Id), present
+	id, _, present := c.Cache.GetOldest()
+	return id, present
 }
 
 func (c *lruIdCache) GetOldestToNewest() (ids []entity.Id) {
-	interfaceKeys := c.lru.Keys()
-	for _, id := range interfaceKeys {
-		ids = append(ids, id.(entity.Id))
-	}
-	return
-}
-
-func (c *lruIdCache) Len() int {
-	return c.lru.Len()
-}
-
-func (c *lruIdCache) Remove(id entity.Id) bool {
-	return c.lru.Remove(id)
+	return c.Keys()
 }

cache/repo_cache_test.go 🔗

@@ -318,7 +318,7 @@ func TestCacheEviction(t *testing.T) {
 	checkBugPresence(t, repoCache, bug2, true)
 	checkBugPresence(t, repoCache, bug3, true)
 
-	// Accessing bug should update position in lruCache and therefore it should not be evicted
+	// Accessing bug should update position in lruCache, and therefore it should not be evicted
 	repoCache.bugs.lru.Get(bug2.Id())
 	oldestId, _ := repoCache.bugs.lru.GetOldest()
 	require.Equal(t, bug3.Id(), oldestId)

cache/subcache.go 🔗

@@ -58,7 +58,7 @@ type SubCache[EntityT entity.Interface, ExcerptT Excerpt, CacheT CacheEntity] st
 	mu       sync.RWMutex
 	excerpts map[entity.Id]ExcerptT
 	cached   map[entity.Id]CacheT
-	lru      *lruIdCache
+	lru      lruIdCache
 }
 
 func NewSubCache[EntityT entity.Interface, ExcerptT Excerpt, CacheT CacheEntity](
@@ -377,7 +377,7 @@ func (sc *SubCache[EntityT, ExcerptT, CacheT]) Resolve(id entity.Id) (CacheT, er
 }
 
 // ResolvePrefix retrieve an entity matching an id prefix. It fails if multiple
-// entity match.
+// entities match.
 func (sc *SubCache[EntityT, ExcerptT, CacheT]) ResolvePrefix(prefix string) (CacheT, error) {
 	return sc.ResolveMatcher(func(excerpt ExcerptT) bool {
 		return excerpt.Id().HasPrefix(prefix)
@@ -406,7 +406,7 @@ func (sc *SubCache[EntityT, ExcerptT, CacheT]) ResolveExcerpt(id entity.Id) (Exc
 }
 
 // ResolveExcerptPrefix retrieve an Excerpt matching an id prefix. It fails if multiple
-// entity match.
+// entities match.
 func (sc *SubCache[EntityT, ExcerptT, CacheT]) ResolveExcerptPrefix(prefix string) (ExcerptT, error) {
 	return sc.ResolveExcerptMatcher(func(excerpt ExcerptT) bool {
 		return excerpt.Id().HasPrefix(prefix)
@@ -629,7 +629,7 @@ func (sc *SubCache[EntityT, ExcerptT, CacheT]) evictIfNeeded() {
 		}
 
 		// as a form of assurance that evicted entities don't get manipulated, we lock them here.
-		// if something try to do it anyway, it will lock the program and make it obvious.
+		// if something tries to do it anyway, it will lock the program and make it obvious.
 		b.Lock()
 
 		sc.lru.Remove(id)

go.mod 🔗

@@ -16,7 +16,7 @@ require (
 	github.com/go-git/go-billy/v5 v5.4.1
 	github.com/go-git/go-git/v5 v5.5.2
 	github.com/gorilla/mux v1.8.0
-	github.com/hashicorp/golang-lru v1.0.2
+	github.com/hashicorp/golang-lru/v2 v2.0.5
 	github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428
 	github.com/mattn/go-isatty v0.0.19
 	github.com/phayes/freeport v0.0.0-20171002181615-b8543db493a5
@@ -45,6 +45,7 @@ require (
 	github.com/VividCortex/ewma v1.2.0 // indirect
 	github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
 	github.com/cloudflare/circl v1.3.3 // indirect
+	github.com/hashicorp/golang-lru v0.5.4 // indirect
 	github.com/lithammer/dedent v1.1.0 // indirect
 	github.com/owenrumney/go-sarif v1.0.11 // indirect
 	github.com/pjbgf/sha1cd v0.2.3 // indirect

go.sum 🔗

@@ -163,9 +163,10 @@ github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxC
 github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
 github.com/hashicorp/go-retryablehttp v0.7.2 h1:AcYqCvkpalPnPF2pn0KamgwamS42TqUDDYFRKq/RAd0=
 github.com/hashicorp/go-retryablehttp v0.7.2/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
+github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
 github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
-github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
-github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
+github.com/hashicorp/golang-lru/v2 v2.0.5 h1:wW7h1TG88eUIJ2i69gaE3uNVtEPIagzhGvHgwfx2Vm4=
+github.com/hashicorp/golang-lru/v2 v2.0.5/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
 github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428 h1:Mo9W14pwbO9VfRe+ygqZ8dFbPpoIK1HFrG/zjTuQ+nc=