perf: use Redis for favicon cache with 24h TTL
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:
parent
352264509c
commit
b57a041b6a
4 changed files with 59 additions and 60 deletions
|
|
@ -77,7 +77,7 @@ func main() {
|
||||||
|
|
||||||
acSvc := autocomplete.NewService(cfg.Upstream.URL, cfg.HTTPTimeout())
|
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()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
|
|
||||||
32
internal/cache/cache.go
vendored
32
internal/cache/cache.go
vendored
|
|
@ -144,6 +144,38 @@ func (c *Cache) Invalidate(ctx context.Context, key string) {
|
||||||
c.client.Del(ctx, fullKey)
|
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.
|
// Close closes the Valkey connection.
|
||||||
func (c *Cache) Close() error {
|
func (c *Cache) Close() error {
|
||||||
if c.client == nil {
|
if c.client == nil {
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,9 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/metamorphosis-dev/samsa/internal/cache"
|
||||||
"github.com/metamorphosis-dev/samsa/internal/contracts"
|
"github.com/metamorphosis-dev/samsa/internal/contracts"
|
||||||
"github.com/metamorphosis-dev/samsa/internal/httpclient"
|
"github.com/metamorphosis-dev/samsa/internal/httpclient"
|
||||||
"github.com/metamorphosis-dev/samsa/internal/search"
|
"github.com/metamorphosis-dev/samsa/internal/search"
|
||||||
|
|
@ -34,16 +34,18 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
searchSvc *search.Service
|
searchSvc *search.Service
|
||||||
autocompleteSvc func(ctx context.Context, query string) ([]string, error)
|
autocompleteSvc func(ctx context.Context, query string) ([]string, error)
|
||||||
sourceURL string
|
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{
|
return &Handler{
|
||||||
searchSvc: searchSvc,
|
searchSvc: searchSvc,
|
||||||
autocompleteSvc: autocompleteSuggestions,
|
autocompleteSvc: autocompleteSuggestions,
|
||||||
sourceURL: sourceURL,
|
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.
|
const faviconCacheTTL = 24 * time.Hour
|
||||||
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
|
|
||||||
|
|
||||||
// Favicon serves a fetched favicon for the given domain, with ETag support
|
// 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.
|
// favicon proxy: the user's browser talks to Kafka, not Google or DuckDuckGo.
|
||||||
func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
|
||||||
domain := strings.TrimPrefix(r.URL.Path, "/favicon/")
|
domain := strings.TrimPrefix(r.URL.Path, "/favicon/")
|
||||||
|
|
@ -195,23 +183,11 @@ func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check cache.
|
cacheKey := "favicon:" + domain
|
||||||
faviconCache.RLock()
|
|
||||||
entry, ok := faviconCache.m[domain]
|
|
||||||
faviconCache.RUnlock()
|
|
||||||
|
|
||||||
now := time.Now()
|
// Check Redis cache.
|
||||||
if ok && now.Sub(entry.cachedAt) < faviconCacheTTL {
|
if cached, ok := h.faviconCache.GetBytes(r.Context(), cacheKey); ok {
|
||||||
// ETag-based cache validation.
|
h.serveFavicon(w, r, cached)
|
||||||
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)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -233,7 +209,6 @@ func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Upstream favicon server issues a redirect or error.
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
http.Error(w, "favicon not found", http.StatusNotFound)
|
http.Error(w, "favicon not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
|
@ -245,34 +220,26 @@ func (h *Handler) Favicon(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
etag := resp.Header.Get("ETag")
|
// Store in Redis with 24h TTL.
|
||||||
if etag == "" {
|
h.faviconCache.SetBytes(r.Context(), cacheKey, body, faviconCacheTTL)
|
||||||
// Fallback ETag: hash of body.
|
|
||||||
h := sha256.Sum256(body)
|
|
||||||
etag = `"` + hex.EncodeToString(h[:8]) + `"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store in cache.
|
h.serveFavicon(w, r, body)
|
||||||
faviconCache.Lock()
|
}
|
||||||
faviconCache.m[domain] = faviconCacheEntry{
|
|
||||||
body: body,
|
// serveFavicon writes a cached or freshly-fetched body with appropriate
|
||||||
etag: etag,
|
// caching headers. ETag is derived from the body hash (no storage needed).
|
||||||
cachedAt: now,
|
func (h *Handler) serveFavicon(w http.ResponseWriter, r *http.Request, body []byte) {
|
||||||
}
|
h2 := sha256.Sum256(body)
|
||||||
faviconCache.Unlock()
|
etag := `"` + hex.EncodeToString(h2[:8]) + `"`
|
||||||
|
|
||||||
if etagMatch := r.Header.Get("If-None-Match"); etagMatch != "" && etagMatch == etag {
|
if etagMatch := r.Header.Get("If-None-Match"); etagMatch != "" && etagMatch == etag {
|
||||||
w.WriteHeader(http.StatusNotModified)
|
w.WriteHeader(http.StatusNotModified)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
contentType := resp.Header.Get("Content-Type")
|
w.Header().Set("Content-Type", "image/x-icon")
|
||||||
if contentType == "" {
|
|
||||||
contentType = "image/x-icon"
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", contentType)
|
|
||||||
w.Header().Set("ETag", etag)
|
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.WriteHeader(http.StatusOK)
|
||||||
w.Write(body)
|
w.Write(body)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ func newTestServer(t *testing.T) (*httptest.Server, *httpapi.Handler) {
|
||||||
EnginesConfig: nil,
|
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 := http.NewServeMux()
|
||||||
mux.HandleFunc("/healthz", h.Healthz)
|
mux.HandleFunc("/healthz", h.Healthz)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue