From 46a2577bf28adf360ce6d74bb1acab03399d2f5c Mon Sep 17 00:00:00 2001 From: partisan Date: Tue, 21 May 2024 10:19:40 +0200 Subject: [PATCH] cache improvements --- text.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/text.go b/text.go index caf9893..0995e0e 100644 --- a/text.go +++ b/text.go @@ -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) - // Always check and cache the next page - go cacheNextPageIfNotCached(query, safe, lang, page+1, resultsPerPage) + // Always check and cache the next page if not enough results + if hasNextPage { + go cacheNextPageIfNotCached(query, safe, lang, page+1, resultsPerPage) + } } 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 currentPage := 1 - resultsNeeded := page * resultsPerPage + resultsNeeded := targetPage * resultsPerPage for len(combinedResults) < resultsNeeded { - results := fetchAndCacheResults(query, safe, lang, currentPage, resultsPerPage) - if len(results) == 0 { + 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) + 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 } - combinedResults = append(combinedResults, results...) - currentPage++ } - startIndex := (page - 1) * resultsPerPage + startIndex := (targetPage - 1) * resultsPerPage endIndex := startIndex + resultsPerPage if startIndex >= len(combinedResults) {