mirror of
https://github.com/taigrr/gico.git
synced 2026-04-02 03:09:07 -07:00
add caching for authors, expauthor type
This commit is contained in:
@@ -15,6 +15,7 @@ var (
|
|||||||
mapTex sync.RWMutex
|
mapTex sync.RWMutex
|
||||||
freqHashCache map[int]map[string]map[string]types.ExpFreq
|
freqHashCache map[int]map[string]map[string]types.ExpFreq
|
||||||
repoHashCache map[int]map[string]map[string]types.ExpRepos
|
repoHashCache map[int]map[string]map[string]types.ExpRepos
|
||||||
|
authorHashCache map[string]types.ExpAuthors
|
||||||
repoCache = make(map[string]types.ExpRepo)
|
repoCache = make(map[string]types.ExpRepo)
|
||||||
// the Repo Cache holds a list of all commits from HEAD back to parent
|
// the Repo Cache holds a list of all commits from HEAD back to parent
|
||||||
// the key is the repo path
|
// the key is the repo path
|
||||||
@@ -26,6 +27,7 @@ func init() {
|
|||||||
freqHashCache = make(map[int]map[string]map[string]types.ExpFreq)
|
freqHashCache = make(map[int]map[string]map[string]types.ExpFreq)
|
||||||
repoHashCache = make(map[int]map[string]map[string]types.ExpRepos)
|
repoHashCache = make(map[int]map[string]map[string]types.ExpRepos)
|
||||||
repoCache = make(map[string]types.ExpRepo)
|
repoCache = make(map[string]types.ExpRepo)
|
||||||
|
authorHashCache = make(map[string]types.ExpAuthors)
|
||||||
}
|
}
|
||||||
|
|
||||||
func hashSlice(in []string) string {
|
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) {
|
func GetCachedRepos(year int, authors, repoPaths []string) ([][]types.Commit, bool) {
|
||||||
a := hashSlice(authors)
|
a := hashSlice(authors)
|
||||||
r := hashSlice(repoPaths)
|
r := hashSlice(repoPaths)
|
||||||
@@ -127,6 +155,7 @@ func CacheRepos(year int, authors, repoPaths []string, commits [][]types.Commit)
|
|||||||
time.Sleep(time.Hour * 1)
|
time.Sleep(time.Hour * 1)
|
||||||
mapTex.Lock()
|
mapTex.Lock()
|
||||||
defer mapTex.Unlock()
|
defer mapTex.Unlock()
|
||||||
|
// optimization, check if the creation time has changed since the last usage
|
||||||
delete(repoHashCache[year][a], r)
|
delete(repoHashCache[year][a], r)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (paths RepoSet) GetRepoAuthors() ([]string, error) {
|
func (paths RepoSet) GetRepoAuthors() ([]string, error) {
|
||||||
|
cache, ok := GetCachedReposAuthors(paths)
|
||||||
|
if ok {
|
||||||
|
return cache, nil
|
||||||
|
}
|
||||||
outChan := make(chan types.Commit, 10)
|
outChan := make(chan types.Commit, 10)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
@@ -45,6 +49,7 @@ func (paths RepoSet) GetRepoAuthors() ([]string, error) {
|
|||||||
a = append(a, k)
|
a = append(a, k)
|
||||||
}
|
}
|
||||||
sort.Strings(a)
|
sort.Strings(a)
|
||||||
|
CacheReposAuthors(paths, a)
|
||||||
return a, nil
|
return a, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ type (
|
|||||||
YearFreq Freq
|
YearFreq Freq
|
||||||
Created time.Time
|
Created time.Time
|
||||||
}
|
}
|
||||||
|
ExpAuthors struct {
|
||||||
|
Authors []string
|
||||||
|
Created time.Time
|
||||||
|
}
|
||||||
ExpRepos struct {
|
ExpRepos struct {
|
||||||
Commits [][]Commit
|
Commits [][]Commit
|
||||||
Created time.Time
|
Created time.Time
|
||||||
|
|||||||
Reference in New Issue
Block a user