cache: add tier definitions and EngineTier function
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
baf98ca80e
commit
ff4149ecbd
2 changed files with 89 additions and 0 deletions
56
internal/cache/tiers.go
vendored
Normal file
56
internal/cache/tiers.go
vendored
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// TTLTier represents a cache TTL tier with a name and duration.
|
||||||
|
type TTLTier struct {
|
||||||
|
Name string
|
||||||
|
Duration time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaultTiers maps engine names to their default TTL tiers.
|
||||||
|
var defaultTiers = map[string]TTLTier{
|
||||||
|
// Static knowledge engines — rarely change
|
||||||
|
"wikipedia": {Name: "static", Duration: 24 * time.Hour},
|
||||||
|
"wikidata": {Name: "static", Duration: 24 * time.Hour},
|
||||||
|
"arxiv": {Name: "static", Duration: 24 * time.Hour},
|
||||||
|
"crossref": {Name: "static", Duration: 24 * time.Hour},
|
||||||
|
"stackoverflow": {Name: "static", Duration: 24 * time.Hour},
|
||||||
|
"github": {Name: "static", Duration: 24 * time.Hour},
|
||||||
|
|
||||||
|
// API-based general search — fresher data
|
||||||
|
"braveapi": {Name: "api_general", Duration: 1 * time.Hour},
|
||||||
|
"youtube": {Name: "api_general", Duration: 1 * time.Hour},
|
||||||
|
|
||||||
|
// Scraped general search — moderately stable
|
||||||
|
"google": {Name: "scraped_general", Duration: 2 * time.Hour},
|
||||||
|
"bing": {Name: "scraped_general", Duration: 2 * time.Hour},
|
||||||
|
"duckduckgo": {Name: "scraped_general", Duration: 2 * time.Hour},
|
||||||
|
"qwant": {Name: "scraped_general", Duration: 2 * time.Hour},
|
||||||
|
"brave": {Name: "scraped_general", Duration: 2 * time.Hour},
|
||||||
|
|
||||||
|
// News/social — changes frequently
|
||||||
|
"reddit": {Name: "news_social", Duration: 30 * time.Minute},
|
||||||
|
|
||||||
|
// Image search
|
||||||
|
"bing_images": {Name: "images", Duration: 1 * time.Hour},
|
||||||
|
"ddg_images": {Name: "images", Duration: 1 * time.Hour},
|
||||||
|
"qwant_images": {Name: "images", Duration: 1 * time.Hour},
|
||||||
|
}
|
||||||
|
|
||||||
|
// EngineTier returns the TTL tier for an engine, applying overrides if provided.
|
||||||
|
// If the engine has no defined tier, returns a default of 1 hour.
|
||||||
|
func EngineTier(engineName string, overrides map[string]time.Duration) TTLTier {
|
||||||
|
// Check override first — override tier name is just the engine name
|
||||||
|
if override, ok := overrides[engineName]; ok && override > 0 {
|
||||||
|
return TTLTier{Name: engineName, Duration: override}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to default tier
|
||||||
|
if tier, ok := defaultTiers[engineName]; ok {
|
||||||
|
return tier
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unknown engines get a sensible default
|
||||||
|
return TTLTier{Name: "unknown", Duration: 1 * time.Hour}
|
||||||
|
}
|
||||||
33
internal/cache/tiers_test.go
vendored
Normal file
33
internal/cache/tiers_test.go
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEngineTier(t *testing.T) {
|
||||||
|
// Test default static tier
|
||||||
|
tier := EngineTier("wikipedia", nil)
|
||||||
|
if tier.Name != "static" || tier.Duration != 24*time.Hour {
|
||||||
|
t.Errorf("wikipedia: expected static/24h, got %s/%v", tier.Name, tier.Duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test default api_general tier
|
||||||
|
tier = EngineTier("braveapi", nil)
|
||||||
|
if tier.Name != "api_general" || tier.Duration != 1*time.Hour {
|
||||||
|
t.Errorf("braveapi: expected api_general/1h, got %s/%v", tier.Name, tier.Duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test override takes precedence — override tier name is just the engine name
|
||||||
|
override := 48 * time.Hour
|
||||||
|
tier = EngineTier("wikipedia", map[string]time.Duration{"wikipedia": override})
|
||||||
|
if tier.Name != "wikipedia" || tier.Duration != 48*time.Hour {
|
||||||
|
t.Errorf("wikipedia override: expected wikipedia/48h, got %s/%v", tier.Name, tier.Duration)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test unknown engine gets default
|
||||||
|
tier = EngineTier("unknown_engine", nil)
|
||||||
|
if tier.Name != "unknown" || tier.Duration != 1*time.Hour {
|
||||||
|
t.Errorf("unknown engine: expected unknown/1h, got %s/%v", tier.Name, tier.Duration)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue