aboutsummaryrefslogtreecommitdiffstats
path: root/cache/lru_id_cache.go
blob: fda12ca6a54b8f86b712ae1eb5e5fa2902f7430d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cache

import (
	"math"

	lru "github.com/hashicorp/golang-lru"

	"github.com/MichaelMure/git-bug/entity"
)

type LRUIdCache struct {
	parentCache *lru.Cache
}

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,
	}
}

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
}

func (c *LRUIdCache) GetOldestToNewest() (ids []entity.Id) {
	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)
}