cache

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package cache implements Redis-backed response caching and cache key generation utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckHealth

func CheckHealth(ctx context.Context, client *redis.Client, timeout time.Duration) error

CheckHealth performs a bounded Redis ping health check.

func NewRedisClient

func NewRedisClient(ctx context.Context, cfg config.RedisConfig) (*redis.Client, error)

NewRedisClient builds and validates a Redis client from runtime config.

Types

type CacheInvalidateConfig

type CacheInvalidateConfig struct {
	// Tags identifies invalidation groups to bump on successful writes.
	Tags []string
	// FailOpen overrides manager-level fail-open policy when set.
	FailOpen *bool
}

CacheInvalidateConfig defines route-level cache invalidation behavior.

type CacheReadConfig

type CacheReadConfig struct {
	// Key optionally overrides route pattern as cache key route segment.
	Key string
	// TTL defines cache entry lifetime and must be greater than zero.
	TTL time.Duration
	// MaxBytes limits cacheable response body bytes for this route.
	MaxBytes int
	// Tags identifies invalidation groups used by CacheInvalidate.
	Tags []string
	// Methods limits cache reads to selected methods (GET/HEAD by default).
	Methods []string
	// CacheStatuses limits cache writes to selected HTTP statuses (200 by default).
	CacheStatuses []int
	// VaryBy defines dynamic cache key dimensions.
	VaryBy CacheVaryBy
	// FailOpen overrides manager-level fail-open policy when set.
	FailOpen *bool
	// AllowAuthenticated marks authenticated responses as cache-eligible when safely varied.
	AllowAuthenticated bool
}

CacheReadConfig defines route-level read-cache behavior.

type CacheVaryBy

type CacheVaryBy struct {
	// Method includes HTTP method in cache key.
	Method bool
	// TenantID includes principal tenant in cache key.
	TenantID bool
	// UserID includes principal user in cache key.
	UserID bool
	// Role includes principal role in cache key.
	Role bool
	// PathParams includes selected route params in cache key.
	PathParams []string
	// QueryParams includes selected query params in cache key.
	QueryParams []string
	// Headers includes selected request headers in cache key.
	Headers []string
}

CacheVaryBy controls which request dimensions participate in cache key generation.

type CachedResponse

type CachedResponse struct {
	// Status is the cached HTTP status code.
	Status int `json:"status"`
	// Body is the cached HTTP response body.
	Body []byte `json:"body"`
	// ContentType is the cached Content-Type header value.
	ContentType string `json:"content_type,omitempty"`
}

CachedResponse stores serialized HTTP response data in Redis.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager encapsulates Redis-backed cache operations and key building.

func NewManager

func NewManager(client redis.UniversalClient, cfg ManagerConfig) (*Manager, error)

NewManager constructs a cache manager with validated defaults.

func (*Manager) BuildReadKey

func (m *Manager) BuildReadKey(ctx context.Context, r *http.Request, route string, cfg CacheReadConfig) (string, error)

BuildReadKey builds a deterministic read key for route cache lookup.

Notes: - Uses low-cardinality route patterns - Supports selected vary dimensions only - Includes tag version token for O(1) invalidation

func (*Manager) BumpTags

func (m *Manager) BumpTags(ctx context.Context, tags []string) error

BumpTags increments tag versions used for cache invalidation.

func (*Manager) DefaultMaxBytes

func (m *Manager) DefaultMaxBytes() int

DefaultMaxBytes returns manager-level fallback cache size cap.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, key string) (CachedResponse, bool, error)

Get fetches and decodes a cached response by key.

func (*Manager) Observe

func (m *Manager) Observe(route, outcome string)

Observe emits cache operation signals to configured observer.

func (*Manager) ResolveFailOpen

func (m *Manager) ResolveFailOpen(override *bool) bool

ResolveFailOpen resolves route override or manager default fail-open policy.

func (*Manager) Set

func (m *Manager) Set(ctx context.Context, key string, value CachedResponse, ttl time.Duration) error

Set stores a cached response for the given key and TTL.

func (*Manager) TagVersionToken

func (m *Manager) TagVersionToken(ctx context.Context, tags []string) (string, error)

TagVersionToken returns a stable token representing current tag versions.

type ManagerConfig

type ManagerConfig struct {
	// Env namespaces cache keys by environment.
	Env string
	// FailOpen controls whether read/write failures should bypass instead of failing requests.
	FailOpen bool
	// DefaultMaxBytes sets fallback response size cap when route does not override MaxBytes.
	DefaultMaxBytes int
	// Observe receives cache outcome signals for metrics.
	Observe ObserveFunc
}

ManagerConfig configures cache manager defaults.

type ObserveFunc

type ObserveFunc func(route, outcome string)

ObserveFunc records cache operation outcomes for metrics integration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL