git-bug/cache/lru_id_cache.go

57 lines
1.0 KiB
Go
Raw Normal View History

2020-07-26 10:52:29 +03:00
package cache
import (
2020-08-25 16:26:23 +03:00
"math"
2020-07-26 10:52:29 +03:00
lru "github.com/hashicorp/golang-lru"
"github.com/MichaelMure/git-bug/entity"
)
2022-11-19 13:33:12 +03:00
type lruIdCache struct {
lru *lru.Cache
2020-07-26 10:52:29 +03:00
}
2022-11-19 13:33:12 +03:00
func newLRUIdCache() *lruIdCache {
// we can ignore the error here as it would only fail if the size is negative.
2020-08-25 16:26:23 +03:00
cache, _ := lru.New(math.MaxInt32)
2020-07-26 10:52:29 +03:00
2022-11-19 13:33:12 +03:00
return &lruIdCache{
2020-07-26 10:52:29 +03:00
cache,
2020-08-25 05:43:42 +03:00
}
2020-07-26 10:52:29 +03:00
}
2022-11-19 13:33:12 +03:00
func (c *lruIdCache) Add(id entity.Id) bool {
return c.lru.Add(id, nil)
2020-07-26 10:52:29 +03:00
}
2022-11-19 13:33:12 +03:00
func (c *lruIdCache) Contains(id entity.Id) bool {
return c.lru.Contains(id)
2020-07-26 10:52:29 +03:00
}
2022-11-19 13:33:12 +03:00
func (c *lruIdCache) Get(id entity.Id) bool {
_, present := c.lru.Get(id)
2020-07-26 10:52:29 +03:00
return present
}
2022-11-19 13:33:12 +03:00
func (c *lruIdCache) GetOldest() (entity.Id, bool) {
id, _, present := c.lru.GetOldest()
2020-07-26 10:52:29 +03:00
return id.(entity.Id), present
}
2022-11-19 13:33:12 +03:00
func (c *lruIdCache) GetOldestToNewest() (ids []entity.Id) {
interfaceKeys := c.lru.Keys()
2020-07-26 10:52:29 +03:00
for _, id := range interfaceKeys {
ids = append(ids, id.(entity.Id))
}
return
}
2022-11-19 13:33:12 +03:00
func (c *lruIdCache) Len() int {
return c.lru.Len()
2020-07-26 10:52:29 +03:00
}
2022-11-19 13:33:12 +03:00
func (c *lruIdCache) Remove(id entity.Id) bool {
return c.lru.Remove(id)
2020-07-26 10:52:29 +03:00
}