diff options
author | Michael Muré <batolettre@gmail.com> | 2020-08-25 15:26:23 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-08-25 15:26:23 +0200 |
commit | 4d678f3e057aea30d1396240d88e622c833a177f (patch) | |
tree | a895db664b6af7d676b6cfd59e001a54e8eae35b /cache/repo_cache.go | |
parent | 4b065029af63c16ffd754ac28190ba4b3125c494 (diff) | |
download | git-bug-4d678f3e057aea30d1396240d88e622c833a177f.tar.gz |
cache: simplify cache eviction
Diffstat (limited to 'cache/repo_cache.go')
-rw-r--r-- | cache/repo_cache.go | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/cache/repo_cache.go b/cache/repo_cache.go index f6ff1abe..d13ce65c 100644 --- a/cache/repo_cache.go +++ b/cache/repo_cache.go @@ -21,7 +21,8 @@ import ( // 2: added cache for identities with a reference in the bug cache const formatVersion = 2 -const lruCacheSize = 100 +// The maximum number of bugs loaded in memory. After that, eviction will be done. +const defaultMaxLoadedBugs = 100 var _ repository.RepoCommon = &RepoCache{} @@ -46,14 +47,16 @@ type RepoCache struct { // the name of the repository, as defined in the MultiRepoCache name string + // maximum number of loaded bugs + maxLoadedBugs int + muBug sync.RWMutex // excerpt of bugs data for all bugs bugExcerpts map[entity.Id]*BugExcerpt // bug loaded in memory bugs map[entity.Id]*BugCache - - // presentBugs is an LRU cache that records which bugs the cache has loaded in - presentBugs *LRUIdCache + // loadedBugs is an LRU cache that records which bugs the cache has loaded in + loadedBugs *LRUIdCache muIdentity sync.RWMutex // excerpt of identities data for all identities @@ -71,10 +74,12 @@ func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) { func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error) { c := &RepoCache{ - repo: r, - name: name, - bugs: make(map[entity.Id]*BugCache), - identities: make(map[entity.Id]*IdentityCache), + repo: r, + name: name, + maxLoadedBugs: defaultMaxLoadedBugs, + bugs: make(map[entity.Id]*BugCache), + loadedBugs: NewLRUIdCache(), + identities: make(map[entity.Id]*IdentityCache), } err := c.lock() @@ -82,9 +87,6 @@ func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error return &RepoCache{}, err } - presentBugs := NewLRUIdCache(lruCacheSize) - c.presentBugs = presentBugs - err = c.load() if err == nil { return c, nil @@ -99,6 +101,12 @@ func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error return c, c.write() } +// setCacheSize change the maximum number of loaded bugs +func (c *RepoCache) setCacheSize(size int) { + c.maxLoadedBugs = size + c.evictIfNeeded() +} + // load will try to read from the disk all the cache files func (c *RepoCache) load() error { err := c.loadBugCache() |