lru_id_cache.go

 1package cache
 2
 3import (
 4	lru "github.com/hashicorp/golang-lru"
 5
 6	"github.com/MichaelMure/git-bug/entity"
 7)
 8
 9type LRUIdCache struct {
10	parentCache *lru.Cache
11	maxSize     int
12}
13
14func NewLRUIdCache(size int) *LRUIdCache {
15	// Ignore error here
16	cache, _ := lru.New(size)
17
18	return &LRUIdCache{
19		cache,
20		size,
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) GetAll() (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}
57
58func (c *LRUIdCache) Resize(size int) int {
59	c.maxSize = size
60	return c.parentCache.Resize(size)
61}