using the cache lmao
This commit is contained in:
parent
d5bbfe118d
commit
bc85d7d4a2
2 changed files with 66 additions and 20 deletions
43
cache.go
43
cache.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TextSearchResult represents a single search result item.
|
||||
|
@ -21,16 +22,24 @@ type CacheKey struct {
|
|||
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
|
||||
// CachedItem represents an item stored in the cache with an expiration time.
|
||||
type CachedItem struct {
|
||||
Results []TextSearchResult
|
||||
StoredTime time.Time
|
||||
}
|
||||
|
||||
// NewResultsCache creates a new ResultsCache.
|
||||
func NewResultsCache() *ResultsCache {
|
||||
// ResultsCache is a thread-safe map for caching search results by composite keys.
|
||||
type ResultsCache struct {
|
||||
mu sync.Mutex
|
||||
results map[string]CachedItem
|
||||
expiration time.Duration
|
||||
}
|
||||
|
||||
// NewResultsCache creates a new ResultsCache with a specified expiration duration.
|
||||
func NewResultsCache(expiration time.Duration) *ResultsCache {
|
||||
return &ResultsCache{
|
||||
results: make(map[string][]TextSearchResult),
|
||||
results: make(map[string]CachedItem),
|
||||
expiration: expiration,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,15 +47,29 @@ func NewResultsCache() *ResultsCache {
|
|||
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
|
||||
|
||||
item, exists := rc.results[rc.keyToString(key)]
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Check if the item has expired
|
||||
if time.Since(item.StoredTime) > rc.expiration {
|
||||
delete(rc.results, rc.keyToString(key))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return item.Results, true
|
||||
}
|
||||
|
||||
// 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
|
||||
rc.results[rc.keyToString(key)] = CachedItem{
|
||||
Results: results,
|
||||
StoredTime: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// keyToString converts a CacheKey to a string representation.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue