aboutsummaryrefslogtreecommitdiffstats
path: root/cache/lru_id_cache.go
blob: 0e5e31a750f51ff97b3dceca92cc77e8de3dae51 (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 {
	lru *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.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
}

func (c *lruIdCache) GetOldest() (entity.Id, bool) {
	id, _, present := c.lru.GetOldest()
	return id.(entity.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)
}