From e723f773f39de0ce152ec31227de3ec898846e2f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:55:10 +0000 Subject: build(deps): bump github.com/hashicorp/golang-lru from 0.5.4 to 1.0.2 Bumps [github.com/hashicorp/golang-lru](https://github.com/hashicorp/golang-lru) from 0.5.4 to 1.0.2. - [Release notes](https://github.com/hashicorp/golang-lru/releases) - [Commits](https://github.com/hashicorp/golang-lru/compare/v0.5.4...v1.0.2) --- updated-dependencies: - dependency-name: github.com/hashicorp/golang-lru dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 41d1a86b..5eda0983 100644 --- a/go.mod +++ b/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 v0.5.4 + github.com/hashicorp/golang-lru v1.0.2 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 diff --git a/go.sum b/go.sum index 7a71e011..e95e3fb4 100644 --- a/go.sum +++ b/go.sum @@ -163,8 +163,9 @@ 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/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= -- cgit From f5e094f2c431f5a03d899b0866692e3534c72066 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Fri, 11 Aug 2023 15:12:28 +0200 Subject: update to golang-lru v2 --- 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(-) diff --git a/cache/lru_id_cache.go b/cache/lru_id_cache.go index 0e5e31a7..b76f5312 100644 --- a/cache/lru_id_cache.go +++ b/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() } diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index 3c11220d..e3a9cc15 100644 --- a/cache/repo_cache_test.go +++ b/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) diff --git a/cache/subcache.go b/cache/subcache.go index b0ba6e52..1306428f 100644 --- a/cache/subcache.go +++ b/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) diff --git a/go.mod b/go.mod index 5eda0983..3754aa9f 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index e95e3fb4..769077b3 100644 --- a/go.sum +++ b/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= -- cgit