Documentation
¶
Overview ¶
Package cache provides caching functionality with Redis and in-memory backends.
Index ¶
- Variables
- type Cache
- type Config
- type MemoryCache
- func (c *MemoryCache) Cleanup()
- func (c *MemoryCache) Clear()
- func (c *MemoryCache) Close() error
- func (c *MemoryCache) Decr(ctx context.Context, key string) (int64, error)
- func (c *MemoryCache) Delete(ctx context.Context, key string) error
- func (c *MemoryCache) DeletePattern(ctx context.Context, pattern string) error
- func (c *MemoryCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *MemoryCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *MemoryCache) GetJSON(ctx context.Context, key string, dest any) error
- func (c *MemoryCache) GetOrSet(ctx context.Context, key string, ttl time.Duration, fn func() (any, error)) (any, error)
- func (c *MemoryCache) Health(ctx context.Context) error
- func (c *MemoryCache) Incr(ctx context.Context, key string) (int64, error)
- func (c *MemoryCache) Keys(ctx context.Context, pattern string) ([]string, error)
- func (c *MemoryCache) MGet(ctx context.Context, keys ...string) ([][]byte, error)
- func (c *MemoryCache) MSet(ctx context.Context, items map[string][]byte, ttl time.Duration) error
- func (c *MemoryCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *MemoryCache) SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *MemoryCache) Stats() Stats
- type Middleware
- func (m *Middleware) Handler(ttl time.Duration) func(http.Handler) http.Handler
- func (m *Middleware) HandlerWithKeyFunc(ttl time.Duration, keyFunc func(*http.Request) string) func(http.Handler) http.Handler
- func (m *Middleware) Invalidate(r *http.Request) error
- func (m *Middleware) InvalidatePattern(pattern string) error
- type RedisCache
- func (c *RedisCache) Close() error
- func (c *RedisCache) Decr(ctx context.Context, key string) (int64, error)
- func (c *RedisCache) Delete(ctx context.Context, key string) error
- func (c *RedisCache) DeletePattern(ctx context.Context, pattern string) error
- func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)
- func (c *RedisCache) GetJSON(ctx context.Context, key string, dest any) error
- func (c *RedisCache) GetOrSet(ctx context.Context, key string, ttl time.Duration, fn func() (any, error)) (any, error)
- func (c *RedisCache) Health(ctx context.Context) error
- func (c *RedisCache) Incr(ctx context.Context, key string) (int64, error)
- func (c *RedisCache) Keys(ctx context.Context, pattern string) ([]string, error)
- func (c *RedisCache) MGet(ctx context.Context, keys ...string) ([][]byte, error)
- func (c *RedisCache) MSet(ctx context.Context, items map[string][]byte, ttl time.Duration) error
- func (c *RedisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *RedisCache) SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *RedisCache) Stats() Stats
- type Stats
Constants ¶
This section is empty.
Variables ¶
var ErrCacheMiss = errors.New("cache miss")
ErrCacheMiss is returned when a key is not found in the cache.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Basic operations
Get(ctx context.Context, key string) ([]byte, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Exists(ctx context.Context, key string) (bool, error)
// Typed operations
GetJSON(ctx context.Context, key string, dest any) error
SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error
// Cache-aside pattern
GetOrSet(ctx context.Context, key string, ttl time.Duration, fn func() (any, error)) (any, error)
// Bulk operations
MGet(ctx context.Context, keys ...string) ([][]byte, error)
MSet(ctx context.Context, items map[string][]byte, ttl time.Duration) error
// Key management
DeletePattern(ctx context.Context, pattern string) error
Keys(ctx context.Context, pattern string) ([]string, error)
// Atomic operations
Incr(ctx context.Context, key string) (int64, error)
Decr(ctx context.Context, key string) (int64, error)
// Lifecycle
Close() error
Health(ctx context.Context) error
// Stats
Stats() Stats
}
Cache defines the interface for cache operations.
type Config ¶
type Config struct {
// Type is the cache backend type: "redis" or "memory"
Type string
// Redis configuration
URL string // Redis URL (redis://localhost:6379)
Password string
DB int
// Cluster configuration
ClusterAddrs []string
ClusterMode bool
// Connection pool settings
PoolSize int
MinIdleConns int
MaxRetries int
// General settings
DefaultTTL time.Duration
Prefix string
// Memory cache settings
MaxMemory int64 // Maximum memory in bytes (0 = unlimited)
MaxItems int // Maximum number of items (0 = unlimited)
}
Config holds cache configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults.
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache implements Cache using an in-memory LRU cache.
func NewMemoryCache ¶
func NewMemoryCache(cfg Config) *MemoryCache
NewMemoryCache creates a new in-memory cache.
func (*MemoryCache) Cleanup ¶
func (c *MemoryCache) Cleanup()
Cleanup triggers an immediate cleanup of expired items. Exposed for testing purposes.
func (*MemoryCache) Close ¶
func (c *MemoryCache) Close() error
Close stops the cleanup goroutine and clears the cache.
func (*MemoryCache) Delete ¶
func (c *MemoryCache) Delete(ctx context.Context, key string) error
Delete removes a key from the cache.
func (*MemoryCache) DeletePattern ¶
func (c *MemoryCache) DeletePattern(ctx context.Context, pattern string) error
DeletePattern deletes all keys matching the glob pattern.
func (*MemoryCache) GetOrSet ¶
func (c *MemoryCache) GetOrSet(ctx context.Context, key string, ttl time.Duration, fn func() (any, error)) (any, error)
GetOrSet implements the cache-aside pattern.
func (*MemoryCache) Health ¶
func (c *MemoryCache) Health(ctx context.Context) error
Health always returns nil for memory cache.
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware provides HTTP caching middleware.
func NewMiddleware ¶
func NewMiddleware(cache Cache) *Middleware
NewMiddleware creates a new caching middleware.
func (*Middleware) HandlerWithKeyFunc ¶
func (m *Middleware) HandlerWithKeyFunc(ttl time.Duration, keyFunc func(*http.Request) string) func(http.Handler) http.Handler
HandlerWithKeyFunc returns a middleware with custom key generation.
func (*Middleware) Invalidate ¶
func (m *Middleware) Invalidate(r *http.Request) error
Invalidate removes a cached response by URL.
func (*Middleware) InvalidatePattern ¶
func (m *Middleware) InvalidatePattern(pattern string) error
InvalidatePattern removes all cached responses matching a pattern.
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements Cache using Redis as the backend.
func NewRedisCache ¶
func NewRedisCache(cfg Config) (*RedisCache, error)
NewRedisCache creates a new Redis-backed cache.
func (*RedisCache) Delete ¶
func (c *RedisCache) Delete(ctx context.Context, key string) error
Delete removes a key from the cache.
func (*RedisCache) DeletePattern ¶
func (c *RedisCache) DeletePattern(ctx context.Context, pattern string) error
DeletePattern deletes all keys matching the pattern.
func (*RedisCache) GetOrSet ¶
func (c *RedisCache) GetOrSet(ctx context.Context, key string, ttl time.Duration, fn func() (any, error)) (any, error)
GetOrSet implements the cache-aside pattern.
func (*RedisCache) Health ¶
func (c *RedisCache) Health(ctx context.Context) error
Health checks if Redis is reachable.