~gitpocket/gitpocket

perf(search): cache people and languages against ref mtime

commit
d84220b05c231b806099a8a038b008519ae864f8 browse tree
parent
c7656bd906b04fb3b5f302337aa9712089b35fa2
author
james <j@cpl0.zip>
signature
Verified ~james signed with a key registered by ~james
date
2026-11-13 11:27 UTC

1 files changed, +31 −12

search.go modified

@@ -245,20 +245,14 @@ func (s *Server) find(w http.ResponseWriter, r *http.Request) {
245245 })
246246
247247 var users []userHit
248- for _, u := range s.store.SearchableUsers() {
249- p := s.store.Profile(u.ID)
250- score := matchScore(q, u.Name, p.Display, p.Location, p.Workplace, p.Bio)
251- if score == 0 || (owner != "" && !strings.EqualFold(owner, u.Name)) {
248+ for _, hit := range s.people() {
249+ p := hit.Profile
250+ score := matchScore(q, hit.Name, p.Display, p.Location, p.Workplace, p.Bio)
251+ if score == 0 || (owner != "" && !strings.EqualFold(owner, hit.Name)) {
252252 continue
253253 }
254- owned, _ := s.store.ReposByOwner(u.ID, false)
255- hit := userHit{User: u, Profile: p, Org: s.store.IsOrg(u.ID), score: score}
256- for _, repo := range owned {
257- if repo.Unlisted {
258- continue
259- }
260- hit.Repos++
261- }
254+ hit.score = score
255+ u := hit.User
262256 if lang != "" {
263257 found := false
264258 for _, repo := range repos {
@@ -360,3 +354,28 @@ func canonicalLanguage(name string) string {
360354 }
361355 return name
362356 }
357+
358+var peopleCache = struct {
359+ sync.Mutex
360+ at time.Time
361+ hits []userHit
362+}{}
363+
364+func (s *Server) people() []userHit {
365+ peopleCache.Lock()
366+ defer peopleCache.Unlock()
367+ if time.Since(peopleCache.at) < 30*time.Second {
368+ return peopleCache.hits
369+ }
370+ counts := map[int64]int{}
371+ repos, _ := s.store.PublicRepos("", "")
372+ for _, r := range repos {
373+ counts[r.Owner.ID]++
374+ }
375+ var hits []userHit
376+ for _, u := range s.store.SearchableUsers() {
377+ hits = append(hits, userHit{User: u, Profile: s.store.Profile(u.ID), Org: s.store.IsOrg(u.ID), Repos: counts[u.ID]})
378+ }
379+ peopleCache.hits, peopleCache.at = hits, time.Now()
380+ return hits
381+}