fix getting wrong search results from cache (when using different search settings)
This commit is contained in:
parent
dade24f476
commit
3f73dcc523
4 changed files with 20 additions and 21 deletions
2
files.go
2
files.go
|
@ -41,7 +41,7 @@ func initializeTorrentSites() {
|
|||
func handleFileSearch(w http.ResponseWriter, settings UserSettings, query string, page int) {
|
||||
startTime := time.Now()
|
||||
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: settings.SafeSearch == "true", Lang: settings.Language, Type: "file"}
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: settings.SafeSearch == "active", Lang: settings.Language, Type: "file"}
|
||||
combinedResults := getFileResultsFromCacheOrFetch(cacheKey, query, settings.SafeSearch, settings.Language, page)
|
||||
|
||||
sort.Slice(combinedResults, func(i, j int) bool { return combinedResults[i].Seeders > combinedResults[j].Seeders })
|
||||
|
|
|
@ -21,7 +21,7 @@ func init() {
|
|||
func handleImageSearch(w http.ResponseWriter, settings UserSettings, query string, page int) {
|
||||
startTime := time.Now()
|
||||
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: settings.SafeSearch == "true", Lang: settings.Language, Type: "image"}
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: settings.SafeSearch == "active", Lang: settings.Language, Type: "image"}
|
||||
combinedResults := getImageResultsFromCacheOrFetch(cacheKey, query, settings.SafeSearch, settings.Language, page)
|
||||
|
||||
elapsedTime := time.Since(startTime)
|
||||
|
|
33
main.go
33
main.go
|
@ -81,37 +81,36 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
|||
settings.Theme = "dark" // Default theme
|
||||
}
|
||||
|
||||
if safe != "" {
|
||||
if safe != "" && safe != settings.SafeSearch {
|
||||
settings.SafeSearch = safe
|
||||
saveUserSettings(w, settings)
|
||||
}
|
||||
|
||||
if lang != "" {
|
||||
if lang != "" && lang != settings.Language {
|
||||
settings.Language = lang
|
||||
saveUserSettings(w, settings)
|
||||
}
|
||||
|
||||
// Render the search page template if no query
|
||||
|
||||
data := struct {
|
||||
LanguageOptions []LanguageOption
|
||||
CurrentLang string
|
||||
Theme string
|
||||
Safe string
|
||||
}{
|
||||
LanguageOptions: languageOptions,
|
||||
CurrentLang: settings.Language,
|
||||
Theme: settings.Theme,
|
||||
Safe: settings.SafeSearch,
|
||||
}
|
||||
// Check if there is a search query
|
||||
if query == "" {
|
||||
// If no query is provided, render the search page template
|
||||
data := struct {
|
||||
LanguageOptions []LanguageOption
|
||||
CurrentLang string
|
||||
Theme string
|
||||
Safe string
|
||||
}{
|
||||
LanguageOptions: languageOptions,
|
||||
CurrentLang: settings.Language,
|
||||
Theme: settings.Theme,
|
||||
Safe: settings.SafeSearch,
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFiles("templates/search.html"))
|
||||
tmpl.Execute(w, data)
|
||||
return
|
||||
}
|
||||
|
||||
settings := loadUserSettings(r)
|
||||
|
||||
// Handle search based on the type
|
||||
switch searchType {
|
||||
case "image":
|
||||
|
|
4
text.go
4
text.go
|
@ -22,7 +22,7 @@ func init() {
|
|||
func HandleTextSearch(w http.ResponseWriter, settings UserSettings, query string, page int) {
|
||||
startTime := time.Now()
|
||||
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: settings.SafeSearch == "true", Lang: settings.Language, Type: "text"}
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: settings.SafeSearch == "active", Lang: settings.Language, Type: "text"}
|
||||
combinedResults := getTextResultsFromCacheOrFetch(cacheKey, query, settings.SafeSearch, settings.Language, page)
|
||||
|
||||
hasPrevPage := page > 1 // dupe
|
||||
|
@ -114,7 +114,7 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
|
|||
}
|
||||
|
||||
func prefetchPage(query, safe, lang string, page int) {
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "active", Lang: lang, Type: "text"}
|
||||
if _, exists := resultsCache.Get(cacheKey); !exists {
|
||||
printInfo("Page %d not cached, caching now...", page)
|
||||
pageResults := fetchTextResults(query, safe, lang, page)
|
||||
|
|
Loading…
Add table
Reference in a new issue