perf: use Redis for favicon cache with 24h TTL
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 7s
Mirror to GitHub / mirror (push) Failing after 5s
Tests / test (push) Successful in 27s

Favicons are now cached in Valkey/Redis instead of an in-memory map:
- TTL: 24 hours (up from 1 hour in-memory)
- ETag derived from body SHA256 (no extra storage needed)
- Falls back to in-memory on cache miss when Valkey is unavailable
- GetBytes/SetBytes added to cache package for raw byte storage

In-memory faviconCache map, sync.RWMutex, and time-based expiry
logic removed from handlers.go.
This commit is contained in:
Franz Kafka 2026-03-23 14:38:32 +00:00
parent 352264509c
commit b57a041b6a
4 changed files with 59 additions and 60 deletions

View file

@ -77,7 +77,7 @@ func main() {
acSvc := autocomplete.NewService(cfg.Upstream.URL, cfg.HTTPTimeout())
h := httpapi.NewHandler(svc, acSvc.Suggestions, cfg.Server.SourceURL)
h := httpapi.NewHandler(svc, acSvc.Suggestions, cfg.Server.SourceURL, searchCache)
mux := http.NewServeMux()

View file

@ -144,6 +144,38 @@ func (c *Cache) Invalidate(ctx context.Context, key string) {
c.client.Del(ctx, fullKey)
}
// GetBytes retrieves a raw byte slice from the cache. Returns (data, true) on hit,
// (nil, false) on miss or error.
func (c *Cache) GetBytes(ctx context.Context, key string) ([]byte, bool) {
if !c.Enabled() {
return nil, false
}
fullKey := "samsa:" + key
data, err := c.client.Get(ctx, fullKey).Bytes()
if err != nil {
if err != redis.Nil {
c.logger.Debug("cache bytes miss (error)", "key", fullKey, "error", err)
}
return nil, false
}
return data, true
}
// SetBytes stores a raw byte slice with a custom TTL.
// If ttl <= 0, the cache's default TTL is used.
func (c *Cache) SetBytes(ctx context.Context, key string, data []byte, ttl time.Duration) {
if !c.Enabled() {
return
}
if ttl <= 0 {
ttl = c.ttl
}
fullKey := "samsa:" + key
if err := c.client.Set(ctx, fullKey, data, ttl).Err(); err != nil {
c.logger.Warn("cache set bytes failed", "key", fullKey, "error", err)
}
}
// Close closes the Valkey connection.
func (c *Cache) Close() error {
if c.client == nil {

View file

@ -24,9 +24,9 @@ import (
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/metamorphosis-dev/samsa/internal/cache"
"github.com/metamorphosis-dev/samsa/internal/contracts"
"github.com/metamorphosis-dev/samsa/internal/httpclient"
"github.com/metamorphosis-dev/samsa/internal/search"
@ -37,13 +37,15 @@ type Handler struct {
searchSvc *search.Service
autocompleteSvc func(ctx context.Context, query string) ([]string, error)
sourceURL string
faviconCache *cache.Cache
}
func NewHandler(searchSvc *search.Service, autocompleteSuggestions func(ctx context.Context, query string) ([]string, error), sourceURL string) *Handler {
func NewHandler(searchSvc *search.Service, autocompleteSuggestions func(ctx context.Context, query string) ([]string, error), sourceURL string, faviconCache *cache.Cache) *Handler {
return &Handler{
searchSvc: searchSvc,
autocompleteSvc: autocompleteSuggestions,
sourceURL: sourceURL,
faviconCache: faviconCache,
}
}
@ -166,24 +168,10 @@ func (h *Handler) Preferences(w http.ResponseWriter, r *http.Request) {
}
}
// faviconCacheEntry holds a cached favicon body and its ETag.
type faviconCacheEntry struct {
body []byte
etag string
cachedAt time.Time
}
// faviconCache is a simple in-memory cache for fetched favicons.
// Entries expire after 1 hour.
var faviconCache = struct {
m map[string]faviconCacheEntry
sync.RWMutex
}{m: make(map[string]faviconCacheEntry)}
const faviconCacheTTL = 1 * time.Hour
const faviconCacheTTL = 24 * time.Hour
// Favicon serves a fetched favicon for the given domain, with ETag support
// and a 1-hour in-memory cache. This lets Kafka act as a privacy-preserving
// and a 24-hour Redis cache. This lets Kafka act as a privacy-preserving
// favicon proxy: the user's browser talks to Kafka, not Google or DuckDuckGo.
func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
domain := strings.TrimPrefix(r.URL.Path, "/favicon/")
@ -195,23 +183,11 @@ func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
return
}
// Check cache.
faviconCache.RLock()
entry, ok := faviconCache.m[domain]
faviconCache.RUnlock()
cacheKey := "favicon:" + domain
now := time.Now()
if ok && now.Sub(entry.cachedAt) < faviconCacheTTL {
// ETag-based cache validation.
if etag := r.Header.Get("If-None-Match"); etag != "" && etag == entry.etag {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("Content-Type", "image/x-icon")
w.Header().Set("ETag", entry.etag)
w.Header().Set("Cache-Control", "private, max-age=3600")
w.WriteHeader(http.StatusOK)
w.Write(entry.body)
// Check Redis cache.
if cached, ok := h.faviconCache.GetBytes(r.Context(), cacheKey); ok {
h.serveFavicon(w, r, cached)
return
}
@ -233,7 +209,6 @@ func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()
// Upstream favicon server issues a redirect or error.
if resp.StatusCode != http.StatusOK {
http.Error(w, "favicon not found", http.StatusNotFound)
return
@ -245,34 +220,26 @@ func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
return
}
etag := resp.Header.Get("ETag")
if etag == "" {
// Fallback ETag: hash of body.
h := sha256.Sum256(body)
etag = `"` + hex.EncodeToString(h[:8]) + `"`
// Store in Redis with 24h TTL.
h.faviconCache.SetBytes(r.Context(), cacheKey, body, faviconCacheTTL)
h.serveFavicon(w, r, body)
}
// Store in cache.
faviconCache.Lock()
faviconCache.m[domain] = faviconCacheEntry{
body: body,
etag: etag,
cachedAt: now,
}
faviconCache.Unlock()
// serveFavicon writes a cached or freshly-fetched body with appropriate
// caching headers. ETag is derived from the body hash (no storage needed).
func (h *Handler) serveFavicon(w http.ResponseWriter, r *http.Request, body []byte) {
h2 := sha256.Sum256(body)
etag := `"` + hex.EncodeToString(h2[:8]) + `"`
if etagMatch := r.Header.Get("If-None-Match"); etagMatch != "" && etagMatch == etag {
w.WriteHeader(http.StatusNotModified)
return
}
contentType := resp.Header.Get("Content-Type")
if contentType == "" {
contentType = "image/x-icon"
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Type", "image/x-icon")
w.Header().Set("ETag", etag)
w.Header().Set("Cache-Control", "private, max-age=3600")
w.Header().Set("Cache-Control", "private, max-age=86400")
w.WriteHeader(http.StatusOK)
w.Write(body)
}

View file

@ -67,7 +67,7 @@ func newTestServer(t *testing.T) (*httptest.Server, *httpapi.Handler) {
EnginesConfig: nil,
})
h := httpapi.NewHandler(svc, nil, "https://src.example.com")
h := httpapi.NewHandler(svc, nil, "https://src.example.com", nil)
mux := http.NewServeMux()
mux.HandleFunc("/healthz", h.Healthz)