caching for "files" and images + moved search result types
This commit is contained in:
parent
5a66f61a4c
commit
7668ee5eca
7 changed files with 253 additions and 124 deletions
76
images.go
76
images.go
|
@ -10,17 +10,6 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// ImageSearchResult represents a single image result
|
||||
type ImageSearchResult struct {
|
||||
Thumbnail string
|
||||
Title string
|
||||
Media string
|
||||
Width int
|
||||
Height int
|
||||
Source string
|
||||
ThumbProxy string
|
||||
}
|
||||
|
||||
// QwantAPIResponse represents the JSON response structure from Qwant API
|
||||
type QwantAPIResponse struct {
|
||||
Data struct {
|
||||
|
@ -116,14 +105,12 @@ func fetchImageResults(query string, safe, lang string, page int) ([]ImageSearch
|
|||
|
||||
// HandleImageSearch is the HTTP handler for image search requests
|
||||
func handleImageSearch(w http.ResponseWriter, query, safe, lang string, page int) {
|
||||
results, err := fetchImageResults(query, safe, lang, page)
|
||||
if err != nil {
|
||||
log.Printf("Error performing image search: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
startTime := time.Now()
|
||||
|
||||
// Parsing the template file with the custom function map
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "image"}
|
||||
combinedResults := getImageResultsFromCacheOrFetch(cacheKey, query, safe, lang, page)
|
||||
|
||||
elapsedTime := time.Since(startTime)
|
||||
tmpl, err := template.New("images.html").Funcs(funcs).ParseFiles("templates/images.html")
|
||||
if err != nil {
|
||||
log.Printf("Error parsing template: %v", err)
|
||||
|
@ -141,14 +128,14 @@ func handleImageSearch(w http.ResponseWriter, query, safe, lang string, page int
|
|||
HasPrevPage bool
|
||||
HasNextPage bool
|
||||
}{
|
||||
Results: results,
|
||||
Results: combinedResults,
|
||||
Query: query,
|
||||
Page: page,
|
||||
Fetched: fmt.Sprintf("%.2f seconds", time.Since(time.Now()).Seconds()),
|
||||
Fetched: fmt.Sprintf("%.2f seconds", elapsedTime.Seconds()),
|
||||
LanguageOptions: languageOptions,
|
||||
CurrentLang: lang,
|
||||
HasPrevPage: page > 1,
|
||||
HasNextPage: len(results) >= 50,
|
||||
HasNextPage: len(combinedResults) >= 50,
|
||||
}
|
||||
|
||||
err = tmpl.Execute(w, data)
|
||||
|
@ -157,3 +144,50 @@ func handleImageSearch(w http.ResponseWriter, query, safe, lang string, page int
|
|||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func getImageResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page int) []ImageSearchResult {
|
||||
cacheChan := make(chan []SearchResult)
|
||||
var combinedResults []ImageSearchResult
|
||||
|
||||
go func() {
|
||||
results, exists := resultsCache.Get(cacheKey)
|
||||
if exists {
|
||||
log.Println("Cache hit")
|
||||
cacheChan <- results
|
||||
} else {
|
||||
log.Println("Cache miss")
|
||||
cacheChan <- nil
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case results := <-cacheChan:
|
||||
if results == nil {
|
||||
combinedResults = fetchAndCacheImageResults(query, safe, lang, page)
|
||||
} else {
|
||||
_, _, imageResults := convertToSpecificResults(results)
|
||||
combinedResults = imageResults
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
log.Println("Cache check timeout")
|
||||
combinedResults = fetchAndCacheImageResults(query, safe, lang, page)
|
||||
}
|
||||
|
||||
return combinedResults
|
||||
}
|
||||
|
||||
func fetchAndCacheImageResults(query, safe, lang string, page int) []ImageSearchResult {
|
||||
results, err := fetchImageResults(query, safe, lang, page)
|
||||
if err != nil || len(results) == 0 {
|
||||
log.Printf("Error fetching image results: %v", err)
|
||||
return []ImageSearchResult{
|
||||
{Title: "Results are currently unavailable, sorry. Please try again later."},
|
||||
}
|
||||
}
|
||||
|
||||
// Cache the valid results
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "image"}
|
||||
resultsCache.Set(cacheKey, convertToSearchResults(results))
|
||||
|
||||
return results
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue