From 009c198cdeae42d1faf7a10bef25e4be8dbf8660 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 21 Feb 2023 21:37:43 -0800 Subject: [PATCH] add caching for authors, expauthor type --- commits/cache.go | 37 +++++++++++++++++++++++++++++++++---- commits/chancommits.go | 5 +++++ types/types.go | 4 ++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/commits/cache.go b/commits/cache.go index 9dfa57f..0c754dd 100644 --- a/commits/cache.go +++ b/commits/cache.go @@ -12,10 +12,11 @@ import ( ) var ( - mapTex sync.RWMutex - freqHashCache map[int]map[string]map[string]types.ExpFreq - repoHashCache map[int]map[string]map[string]types.ExpRepos - repoCache = make(map[string]types.ExpRepo) + mapTex sync.RWMutex + freqHashCache map[int]map[string]map[string]types.ExpFreq + repoHashCache map[int]map[string]map[string]types.ExpRepos + authorHashCache map[string]types.ExpAuthors + repoCache = make(map[string]types.ExpRepo) // the Repo Cache holds a list of all commits from HEAD back to parent // the key is the repo path // if the hash of the first commit / HEAD commit doesn't match the current HEAD, @@ -26,6 +27,7 @@ func init() { freqHashCache = make(map[int]map[string]map[string]types.ExpFreq) repoHashCache = make(map[int]map[string]map[string]types.ExpRepos) repoCache = make(map[string]types.ExpRepo) + authorHashCache = make(map[string]types.ExpAuthors) } func hashSlice(in []string) string { @@ -87,6 +89,32 @@ func CacheRepo(path string, commits []types.Commit) { }() } +func CacheReposAuthors(paths []string, authors []string) { + r := hashSlice(paths) + mapTex.Lock() + defer mapTex.Unlock() + authorHashCache[r] = types.ExpAuthors{Authors: authors, Created: time.Now()} + go func() { + time.Sleep(time.Hour * 1) + mapTex.Lock() + defer mapTex.Unlock() + delete(authorHashCache, r) + }() +} + +func GetCachedReposAuthors(paths []string) ([]string, bool) { + r := hashSlice(paths) + mapTex.RLock() + defer mapTex.RUnlock() + if m1, ok := authorHashCache[r]; !ok { + return []string{}, false + } else if m1.Created.Before(time.Now().Add(time.Minute * -15)) { + return []string{}, false + } else { + return m1.Authors, true + } +} + func GetCachedRepos(year int, authors, repoPaths []string) ([][]types.Commit, bool) { a := hashSlice(authors) r := hashSlice(repoPaths) @@ -127,6 +155,7 @@ func CacheRepos(year int, authors, repoPaths []string, commits [][]types.Commit) time.Sleep(time.Hour * 1) mapTex.Lock() defer mapTex.Unlock() + // optimization, check if the creation time has changed since the last usage delete(repoHashCache[year][a], r) }() } diff --git a/commits/chancommits.go b/commits/chancommits.go index f5c5188..61f6f59 100644 --- a/commits/chancommits.go +++ b/commits/chancommits.go @@ -12,6 +12,10 @@ import ( ) func (paths RepoSet) GetRepoAuthors() ([]string, error) { + cache, ok := GetCachedReposAuthors(paths) + if ok { + return cache, nil + } outChan := make(chan types.Commit, 10) var wg sync.WaitGroup for _, p := range paths { @@ -45,6 +49,7 @@ func (paths RepoSet) GetRepoAuthors() ([]string, error) { a = append(a, k) } sort.Strings(a) + CacheReposAuthors(paths, a) return a, nil } diff --git a/types/types.go b/types/types.go index 312f854..ddf5478 100644 --- a/types/types.go +++ b/types/types.go @@ -29,6 +29,10 @@ type ( YearFreq Freq Created time.Time } + ExpAuthors struct { + Authors []string + Created time.Time + } ExpRepos struct { Commits [][]Commit Created time.Time