diff options
author | vince <vincetiu8@gmail.com> | 2020-07-26 15:52:29 +0800 |
---|---|---|
committer | vince <vincetiu8@gmail.com> | 2020-08-20 14:06:18 +0800 |
commit | 6efada43e73c40e0c76c441f84cf02cc00d3eb1b (patch) | |
tree | ae1aef821a8447d299ad9d1afc25809f4ccde3f8 /cache/repo_cache.go | |
parent | 88c28db99851e7f5cceed6544759d37ac87a34d4 (diff) | |
download | git-bug-6efada43e73c40e0c76c441f84cf02cc00d3eb1b.tar.gz |
Implement the LRU Cache
Diffstat (limited to 'cache/repo_cache.go')
-rw-r--r-- | cache/repo_cache.go | 20 |
1 files changed, 18 insertions, 2 deletions
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 } |