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) {
|
func handleFileSearch(w http.ResponseWriter, settings UserSettings, query string, page int) {
|
||||||
startTime := time.Now()
|
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)
|
combinedResults := getFileResultsFromCacheOrFetch(cacheKey, query, settings.SafeSearch, settings.Language, page)
|
||||||
|
|
||||||
sort.Slice(combinedResults, func(i, j int) bool { return combinedResults[i].Seeders > combinedResults[j].Seeders })
|
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) {
|
func handleImageSearch(w http.ResponseWriter, settings UserSettings, query string, page int) {
|
||||||
startTime := time.Now()
|
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)
|
combinedResults := getImageResultsFromCacheOrFetch(cacheKey, query, settings.SafeSearch, settings.Language, page)
|
||||||
|
|
||||||
elapsedTime := time.Since(startTime)
|
elapsedTime := time.Since(startTime)
|
||||||
|
|
13
main.go
13
main.go
|
@ -81,18 +81,19 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
settings.Theme = "dark" // Default theme
|
settings.Theme = "dark" // Default theme
|
||||||
}
|
}
|
||||||
|
|
||||||
if safe != "" {
|
if safe != "" && safe != settings.SafeSearch {
|
||||||
settings.SafeSearch = safe
|
settings.SafeSearch = safe
|
||||||
saveUserSettings(w, settings)
|
saveUserSettings(w, settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
if lang != "" {
|
if lang != "" && lang != settings.Language {
|
||||||
settings.Language = lang
|
settings.Language = lang
|
||||||
saveUserSettings(w, settings)
|
saveUserSettings(w, settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the search page template if no query
|
// Check if there is a search query
|
||||||
|
if query == "" {
|
||||||
|
// If no query is provided, render the search page template
|
||||||
data := struct {
|
data := struct {
|
||||||
LanguageOptions []LanguageOption
|
LanguageOptions []LanguageOption
|
||||||
CurrentLang string
|
CurrentLang string
|
||||||
|
@ -104,14 +105,12 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
Theme: settings.Theme,
|
Theme: settings.Theme,
|
||||||
Safe: settings.SafeSearch,
|
Safe: settings.SafeSearch,
|
||||||
}
|
}
|
||||||
if query == "" {
|
|
||||||
tmpl := template.Must(template.ParseFiles("templates/search.html"))
|
tmpl := template.Must(template.ParseFiles("templates/search.html"))
|
||||||
tmpl.Execute(w, data)
|
tmpl.Execute(w, data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
settings := loadUserSettings(r)
|
|
||||||
|
|
||||||
// Handle search based on the type
|
// Handle search based on the type
|
||||||
switch searchType {
|
switch searchType {
|
||||||
case "image":
|
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) {
|
func HandleTextSearch(w http.ResponseWriter, settings UserSettings, query string, page int) {
|
||||||
startTime := time.Now()
|
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)
|
combinedResults := getTextResultsFromCacheOrFetch(cacheKey, query, settings.SafeSearch, settings.Language, page)
|
||||||
|
|
||||||
hasPrevPage := page > 1 // dupe
|
hasPrevPage := page > 1 // dupe
|
||||||
|
@ -114,7 +114,7 @@ func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string,
|
||||||
}
|
}
|
||||||
|
|
||||||
func prefetchPage(query, safe, lang string, page int) {
|
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 {
|
if _, exists := resultsCache.Get(cacheKey); !exists {
|
||||||
printInfo("Page %d not cached, caching now...", page)
|
printInfo("Page %d not cached, caching now...", page)
|
||||||
pageResults := fetchTextResults(query, safe, lang, page)
|
pageResults := fetchTextResults(query, safe, lang, page)
|
||||||
|
|
Loading…
Add table
Reference in a new issue