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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LRUIdCache struct {
|
|
|
|
parentCache *lru.Cache
|
|
|
|
}
|
|
|
|
|
2020-08-25 16:26:23 +03:00
|
|
|
func NewLRUIdCache() *LRUIdCache {
|
2020-08-25 05:43:42 +03:00
|
|
|
// Ignore error here
|
2020-08-25 16:26:23 +03:00
|
|
|
cache, _ := lru.New(math.MaxInt32)
|
2020-07-26 10:52:29 +03:00
|
|
|
|
|
|
|
return &LRUIdCache{
|
|
|
|
cache,
|
2020-08-25 05:43:42 +03:00
|
|
|
}
|
2020-07-26 10:52:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LRUIdCache) Add(id entity.Id) bool {
|
|
|
|
return c.parentCache.Add(id, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LRUIdCache) Contains(id entity.Id) bool {
|
|
|
|
return c.parentCache.Contains(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LRUIdCache) Get(id entity.Id) bool {
|
|
|
|
_, present := c.parentCache.Get(id)
|
|
|
|
return present
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LRUIdCache) GetOldest() (entity.Id, bool) {
|
|
|
|
id, _, present := c.parentCache.GetOldest()
|
|
|
|
return id.(entity.Id), present
|
|
|
|
}
|
|
|
|
|
2020-08-25 16:26:23 +03:00
|
|
|
func (c *LRUIdCache) GetOldestToNewest() (ids []entity.Id) {
|
2020-07-26 10:52:29 +03:00
|
|
|
interfaceKeys := c.parentCache.Keys()
|
|
|
|
for _, id := range interfaceKeys {
|
|
|
|
ids = append(ids, id.(entity.Id))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LRUIdCache) Len() int {
|
|
|
|
return c.parentCache.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LRUIdCache) Remove(id entity.Id) bool {
|
|
|
|
return c.parentCache.Remove(id)
|
|
|
|
}
|