cache improvements

This commit is contained in:
partisan 2024-05-21 10:19:40 +02:00
parent 84d790a56e
commit 46a2577bf2

33
text.go
View file

@ -33,8 +33,10 @@ func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int)
displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds(), page, hasPrevPage, hasNextPage) displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds(), page, hasPrevPage, hasNextPage)
// Always check and cache the next page // Always check and cache the next page if not enough results
go cacheNextPageIfNotCached(query, safe, lang, page+1, resultsPerPage) if hasNextPage {
go cacheNextPageIfNotCached(query, safe, lang, page+1, resultsPerPage)
}
} }
func getResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page, resultsPerPage int) []TextSearchResult { func getResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page, resultsPerPage int) []TextSearchResult {
@ -78,21 +80,34 @@ func cacheNextPageIfNotCached(query, safe, lang string, page, resultsPerPage int
} }
} }
func fetchResultsUntilFull(query, safe, lang string, page, resultsPerPage int) []TextSearchResult { func fetchResultsUntilFull(query, safe, lang string, targetPage, resultsPerPage int) []TextSearchResult {
var combinedResults []TextSearchResult var combinedResults []TextSearchResult
currentPage := 1 currentPage := 1
resultsNeeded := page * resultsPerPage resultsNeeded := targetPage * resultsPerPage
for len(combinedResults) < resultsNeeded { for len(combinedResults) < resultsNeeded {
results := fetchAndCacheResults(query, safe, lang, currentPage, resultsPerPage) cacheKey := CacheKey{Query: query, Page: currentPage, Safe: safe, Lang: lang}
if len(results) == 0 { cachedResults, exists := resultsCache.Get(cacheKey)
if exists {
combinedResults = append(combinedResults, cachedResults...)
} else {
results := fetchAndCacheResults(query, safe, lang, currentPage, resultsPerPage)
if len(results) == 0 {
break
}
combinedResults = append(combinedResults, results...)
resultsCache.Set(cacheKey, results)
}
currentPage++
// Stop fetching if we have enough results for the target page and the next page
if len(combinedResults) >= resultsNeeded+resultsPerPage {
break break
} }
combinedResults = append(combinedResults, results...)
currentPage++
} }
startIndex := (page - 1) * resultsPerPage startIndex := (targetPage - 1) * resultsPerPage
endIndex := startIndex + resultsPerPage endIndex := startIndex + resultsPerPage
if startIndex >= len(combinedResults) { if startIndex >= len(combinedResults) {