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 ++++---- 3 files changed, 14 insertions(+), 39 deletions(-) (limited to 'cache') 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) -- cgit