caching for "files" and images + moved search result types

This commit is contained in:
partisan 2024-05-24 14:07:16 +02:00
parent 5a66f61a4c
commit 7668ee5eca
7 changed files with 253 additions and 124 deletions

102
cache.go
View file

@ -1,3 +1,4 @@
// common_cache.go
package main
import (
@ -6,7 +7,12 @@ import (
"time"
)
// TextSearchResult represents a single search result item.
var resultsCache = NewResultsCache(6 * time.Hour) // Cache with 6-hour expiration
// SearchResult is a generic interface for all types of search results.
type SearchResult interface{}
// Define various search result types implementing SearchResult interface
type TextSearchResult struct {
URL string
Header string
@ -14,17 +20,59 @@ type TextSearchResult struct {
Source string
}
type ImageSearchResult struct {
Thumbnail string
Title string
Media string
Width int
Height int
Source string
ThumbProxy string
}
type VideoResult struct {
Href string
Title string
Date string
Views string
Creator string
Publisher string
Image string
Duration string
}
type TorrentResult struct {
URL string
Seeders int
Leechers int
Magnet string
Views int
Size string
Title string
Error string
}
type ForumSearchResult struct {
URL string `json:"url"`
Header string `json:"header"`
Description string `json:"description"`
PublishedDate time.Time `json:"publishedDate"`
ImgSrc string `json:"imgSrc,omitempty"`
ThumbnailSrc string `json:"thumbnailSrc,omitempty"`
}
// CacheKey represents the key used to store search results in the cache.
type CacheKey struct {
Query string
Page int
Safe string
Safe bool
Lang string
Type string
}
// CachedItem represents an item stored in the cache with an expiration time.
type CachedItem struct {
Results []TextSearchResult
Results []SearchResult
StoredTime time.Time
}
@ -44,7 +92,7 @@ func NewResultsCache(expiration time.Duration) *ResultsCache {
}
// Get retrieves the results for a given key from the cache.
func (rc *ResultsCache) Get(key CacheKey) ([]TextSearchResult, bool) {
func (rc *ResultsCache) Get(key CacheKey) ([]SearchResult, bool) {
rc.mu.Lock()
defer rc.mu.Unlock()
@ -63,7 +111,7 @@ func (rc *ResultsCache) Get(key CacheKey) ([]TextSearchResult, bool) {
}
// Set stores the results for a given key in the cache.
func (rc *ResultsCache) Set(key CacheKey, results []TextSearchResult) {
func (rc *ResultsCache) Set(key CacheKey, results []SearchResult) {
rc.mu.Lock()
defer rc.mu.Unlock()
rc.results[rc.keyToString(key)] = CachedItem{
@ -74,5 +122,47 @@ func (rc *ResultsCache) Set(key CacheKey, results []TextSearchResult) {
// keyToString converts a CacheKey to a string representation.
func (rc *ResultsCache) keyToString(key CacheKey) string {
return fmt.Sprintf("%s|%d|%s|%s", key.Query, key.Page, key.Safe, key.Lang)
return fmt.Sprintf("%s|%d|%t|%s|%s", key.Query, key.Page, key.Safe, key.Lang, key.Type)
}
// Helper functions to convert between generic SearchResult and specific ImageSearchResult
func convertToSearchResults(results interface{}) []SearchResult {
switch res := results.(type) {
case []TextSearchResult:
genericResults := make([]SearchResult, len(res))
for i, r := range res {
genericResults[i] = r
}
return genericResults
case []TorrentResult:
genericResults := make([]SearchResult, len(res))
for i, r := range res {
genericResults[i] = r
}
return genericResults
case []ImageSearchResult:
genericResults := make([]SearchResult, len(res))
for i, r := range res {
genericResults[i] = r
}
return genericResults
}
return nil
}
func convertToSpecificResults(results []SearchResult) ([]TextSearchResult, []TorrentResult, []ImageSearchResult) {
var textResults []TextSearchResult
var torrentResults []TorrentResult
var imageResults []ImageSearchResult
for _, r := range results {
switch res := r.(type) {
case TextSearchResult:
textResults = append(textResults, res)
case TorrentResult:
torrentResults = append(torrentResults, res)
case ImageSearchResult:
imageResults = append(imageResults, res)
}
}
return textResults, torrentResults, imageResults
}