cache improvements
This commit is contained in:
parent
c78c8f98fb
commit
305474936b
1 changed files with 24 additions and 9 deletions
25
text.go
25
text.go
|
@ -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)
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
func getResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page, resultsPerPage int) []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
|
||||
currentPage := 1
|
||||
resultsNeeded := page * resultsPerPage
|
||||
resultsNeeded := targetPage * resultsPerPage
|
||||
|
||||
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)
|
||||
if len(results) == 0 {
|
||||
break
|
||||
}
|
||||
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
|
||||
|
||||
if startIndex >= len(combinedResults) {
|
||||
|
|
Loading…
Add table
Reference in a new issue