From 6efada43e73c40e0c76c441f84cf02cc00d3eb1b Mon Sep 17 00:00:00 2001 From: vince Date: Sun, 26 Jul 2020 15:52:29 +0800 Subject: Implement the LRU Cache --- cache/repo_cache.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'cache/repo_cache.go') diff --git a/cache/repo_cache.go b/cache/repo_cache.go index 89772455..5fe1b798 100644 --- a/cache/repo_cache.go +++ b/cache/repo_cache.go @@ -21,6 +21,8 @@ import ( // 2: added cache for identities with a reference in the bug cache const formatVersion = 2 +const lruCacheSize = 100 + var _ repository.RepoCommon = &RepoCache{} // RepoCache is a cache for a Repository. This cache has multiple functions: @@ -50,6 +52,9 @@ type RepoCache struct { // 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 + muIdentity sync.RWMutex // excerpt of identities data for all identities identitiesExcerpts map[entity.Id]*IdentityExcerpt @@ -144,7 +149,7 @@ func (c *RepoCache) Close() error { c.identities = make(map[entity.Id]*IdentityCache) c.identitiesExcerpts = nil - c.bugs = make(map[entity.Id]*BugCache) + c.bugs = nil c.bugExcerpts = nil lockPath := repoLockFilePath(c.repo) @@ -175,11 +180,22 @@ func (c *RepoCache) buildCache() error { _, _ = fmt.Fprintf(os.Stderr, "Building bug cache... ") + presentBugs, err := NewLRUIdCache(lruCacheSize, c.onEvict) + if err != nil { + return err + } + c.presentBugs = presentBugs + c.bugExcerpts = make(map[entity.Id]*BugExcerpt) allBugs := bug.ReadAllLocalBugs(c.repo) - for b := range allBugs { + for i := 0; i < lruCacheSize; i++ { + if len(allBugs) == 0 { + break + } + + b := <-allBugs if b.Err != nil { return b.Err } -- cgit