mirror of
https://github.com/taigrr/gico.git
synced 2026-04-01 18:58:59 -07:00
add caching for authors, expauthor type
This commit is contained in:
@@ -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)
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,10 @@ type (
|
||||
YearFreq Freq
|
||||
Created time.Time
|
||||
}
|
||||
ExpAuthors struct {
|
||||
Authors []string
|
||||
Created time.Time
|
||||
}
|
||||
ExpRepos struct {
|
||||
Commits [][]Commit
|
||||
Created time.Time
|
||||
|
||||
Reference in New Issue
Block a user