cache improvements

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

25
text.go
View file

@ -33,9 +33,11 @@ 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
if hasNextPage {
go cacheNextPageIfNotCached(query, safe, lang, page+1, resultsPerPage) 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 {
cacheChan := make(chan []TextSearchResult) cacheChan := make(chan []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 {
cacheKey := CacheKey{Query: query, Page: currentPage, Safe: safe, Lang: lang}
cachedResults, exists := resultsCache.Get(cacheKey)
if exists {
combinedResults = append(combinedResults, cachedResults...)
} else {
results := fetchAndCacheResults(query, safe, lang, currentPage, resultsPerPage) results := fetchAndCacheResults(query, safe, lang, currentPage, resultsPerPage)
if len(results) == 0 { if len(results) == 0 {
break break
} }
combinedResults = append(combinedResults, results...) combinedResults = append(combinedResults, results...)
currentPage++ resultsCache.Set(cacheKey, results)
} }
startIndex := (page - 1) * resultsPerPage currentPage++
// Stop fetching if we have enough results for the target page and the next page
if len(combinedResults) >= resultsNeeded+resultsPerPage {
break
}
}
startIndex := (targetPage - 1) * resultsPerPage
endIndex := startIndex + resultsPerPage endIndex := startIndex + resultsPerPage
if startIndex >= len(combinedResults) { if startIndex >= len(combinedResults) {