Skip to content

Commit

Permalink
add a cache for the call history database
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed Oct 21, 2023
1 parent 33919fb commit d04bf47
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions core/callhistory/callhistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func New(settings core.Settings, callback AvailabilityCallback) *Finder {
}

result.database = loadCallHistory(result.filename)
result.cache = make(map[string][]scp.Match)
result.callback(core.CallHistoryService, result.Available())
if result.Available() {
log.Printf("Using call history from %s", result.filename)
Expand All @@ -39,6 +40,7 @@ func New(settings core.Settings, callback AvailabilityCallback) *Finder {

type Finder struct {
database *scp.Database
cache map[string][]scp.Match
callback AvailabilityCallback

filename string
Expand All @@ -60,6 +62,7 @@ func (f *Finder) ContestChanged(contest core.Contest) {
f.fieldNames = contest.CallHistoryFieldNames

f.database = loadCallHistory(f.filename)
f.cache = make(map[string][]scp.Match)
f.callback(core.CallHistoryService, f.Available())
if f.Available() {
log.Printf("Using call history from %s", f.filename)
Expand All @@ -77,7 +80,7 @@ func (f *Finder) FindEntry(s string) (core.AnnotatedCallsign, bool) {
}
searchString := searchCallsign.String()

entries, err := f.database.Find(searchString)
entries, err := f.findInDatabase(searchString)
if err != nil {
log.Print(err)
return core.AnnotatedCallsign{}, false
Expand Down Expand Up @@ -106,7 +109,7 @@ func (f *Finder) Find(s string) ([]core.AnnotatedCallsign, error) {
return nil, fmt.Errorf("the call history is currently not available")
}

matches, err := f.database.Find(s)
matches, err := f.findInDatabase(s)
if err != nil {
return nil, err
}
Expand All @@ -128,6 +131,20 @@ func (f *Finder) Find(s string) ([]core.AnnotatedCallsign, error) {
return result, nil
}

func (f *Finder) findInDatabase(s string) ([]scp.Match, error) {
cached, hit := f.cache[s]
if hit {
return cached, nil
}

matches, err := f.database.Find(s)
if err != nil {
return nil, err
}
f.cache[s] = matches
return matches, nil
}

func toAnnotatedCallsign(match scp.Match) (core.AnnotatedCallsign, error) {
cs, err := callsign.Parse(match.Key())
if err != nil {
Expand Down

0 comments on commit d04bf47

Please sign in to comment.