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
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)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue