cache v1 + debug mode + buttons on text results

This commit is contained in:
partisan 2024-05-19 22:57:23 +02:00
parent 9208104ff7
commit d5bbfe118d
9 changed files with 228 additions and 65 deletions

55
cache.go Normal file
View file

@ -0,0 +1,55 @@
package main
import (
"fmt"
"sync"
)
// TextSearchResult represents a single search result item.
type TextSearchResult struct {
URL string
Header string
Description string
Source string
}
// CacheKey represents the key used to store search results in the cache.
type CacheKey struct {
Query string
Page int
Safe string
Lang string
}
// ResultsCache is a thread-safe map for caching search results by composite keys.
type ResultsCache struct {
mu sync.Mutex
results map[string][]TextSearchResult
}
// NewResultsCache creates a new ResultsCache.
func NewResultsCache() *ResultsCache {
return &ResultsCache{
results: make(map[string][]TextSearchResult),
}
}
// Get retrieves the results for a given key from the cache.
func (rc *ResultsCache) Get(key CacheKey) ([]TextSearchResult, bool) {
rc.mu.Lock()
defer rc.mu.Unlock()
results, exists := rc.results[rc.keyToString(key)]
return results, exists
}
// Set stores the results for a given key in the cache.
func (rc *ResultsCache) Set(key CacheKey, results []TextSearchResult) {
rc.mu.Lock()
defer rc.mu.Unlock()
rc.results[rc.keyToString(key)] = results
}
// 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)
}