feat: Valkey cache for search results
- Add internal/cache package using go-redis/v9 (Valkey-compatible) - Cache keys are deterministic SHA-256 hashes of search parameters - Cache wraps the Search() method: check cache → miss → execute → store - Gracefully disabled if Valkey is unreachable or unconfigured - Configurable TTL (default 5m), address, password, and DB index - Environment variable overrides: VALKEY_ADDRESS, VALKEY_PASSWORD, VALKEY_DB, VALKEY_CACHE_TTL - Structured JSON logging via slog throughout cache layer - Refactored service.go: extract executeSearch() from Search() for clarity - Update config.example.toml with [cache] section - Add cache package tests (key generation, nop behavior)
This commit is contained in:
parent
385a7acab7
commit
94322ceff4
9 changed files with 361 additions and 9 deletions
|
|
@ -4,9 +4,11 @@ import (
|
|||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/ashie/gosearch/internal/cache"
|
||||
"github.com/ashie/gosearch/internal/config"
|
||||
"github.com/ashie/gosearch/internal/httpapi"
|
||||
"github.com/ashie/gosearch/internal/search"
|
||||
|
|
@ -16,11 +18,24 @@ func main() {
|
|||
configPath := flag.String("config", "config.toml", "path to config.toml")
|
||||
flag.Parse()
|
||||
|
||||
// Initialize structured logging.
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
cfg, err := config.Load(*configPath)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
// Initialize Valkey cache.
|
||||
searchCache := cache.New(cache.Config{
|
||||
Address: cfg.Cache.Address,
|
||||
Password: cfg.Cache.Password,
|
||||
DB: cfg.Cache.DB,
|
||||
DefaultTTL: cfg.CacheTTL(),
|
||||
}, logger)
|
||||
defer searchCache.Close()
|
||||
|
||||
// Seed env vars from config so existing engine/factory/planner code
|
||||
// picks them up without changes. The config layer is the single source
|
||||
// of truth; env vars remain as overrides via applyEnvOverrides.
|
||||
|
|
@ -37,6 +52,7 @@ func main() {
|
|||
svc := search.NewService(search.ServiceConfig{
|
||||
UpstreamURL: cfg.Upstream.URL,
|
||||
HTTPTimeout: cfg.HTTPTimeout(),
|
||||
Cache: searchCache,
|
||||
})
|
||||
|
||||
h := httpapi.NewHandler(svc)
|
||||
|
|
@ -46,6 +62,6 @@ func main() {
|
|||
mux.HandleFunc("/search", h.Search)
|
||||
|
||||
addr := fmt.Sprintf(":%d", cfg.Server.Port)
|
||||
log.Printf("searxng-go listening on %s", addr)
|
||||
logger.Info("searxng-go starting", "addr", addr, "cache", searchCache.Enabled())
|
||||
log.Fatal(http.ListenAndServe(addr, mux))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue