1package cache
2
3import (
4 "math"
5
6 lru "github.com/hashicorp/golang-lru"
7
8 "github.com/MichaelMure/git-bug/entity"
9)
10
11type LRUIdCache struct {
12 parentCache *lru.Cache
13}
14
15func NewLRUIdCache() *LRUIdCache {
16 // we can ignore the error here as it would only fail if the size is negative.
17 cache, _ := lru.New(math.MaxInt32)
18
19 return &LRUIdCache{
20 cache,
21 }
22}
23
24func (c *LRUIdCache) Add(id entity.Id) bool {
25 return c.parentCache.Add(id, nil)
26}
27
28func (c *LRUIdCache) Contains(id entity.Id) bool {
29 return c.parentCache.Contains(id)
30}
31
32func (c *LRUIdCache) Get(id entity.Id) bool {
33 _, present := c.parentCache.Get(id)
34 return present
35}
36
37func (c *LRUIdCache) GetOldest() (entity.Id, bool) {
38 id, _, present := c.parentCache.GetOldest()
39 return id.(entity.Id), present
40}
41
42func (c *LRUIdCache) GetOldestToNewest() (ids []entity.Id) {
43 interfaceKeys := c.parentCache.Keys()
44 for _, id := range interfaceKeys {
45 ids = append(ids, id.(entity.Id))
46 }
47 return
48}
49
50func (c *LRUIdCache) Len() int {
51 return c.parentCache.Len()
52}
53
54func (c *LRUIdCache) Remove(id entity.Id) bool {
55 return c.parentCache.Remove(id)
56}