cache

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cache provides caching functionality with Redis and in-memory backends.

Index

Constants

This section is empty.

Variables

View Source
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.

func New

func New(cfg Config) (Cache, error)

New creates a new cache instance based on configuration.

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) Clear

func (c *MemoryCache) Clear()

Clear removes all items from the cache.

func (*MemoryCache) Close

func (c *MemoryCache) Close() error

Close stops the cleanup goroutine and clears the cache.

func (*MemoryCache) Decr

func (c *MemoryCache) Decr(ctx context.Context, key string) (int64, error)

Decr decrements a key's value.

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) Exists

func (c *MemoryCache) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists in the cache.

func (*MemoryCache) Get

func (c *MemoryCache) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value from the cache.

func (*MemoryCache) GetJSON

func (c *MemoryCache) GetJSON(ctx context.Context, key string, dest any) error

GetJSON retrieves and unmarshals a JSON value from the cache.

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.

func (*MemoryCache) Incr

func (c *MemoryCache) Incr(ctx context.Context, key string) (int64, error)

Incr increments a key's value.

func (*MemoryCache) Keys

func (c *MemoryCache) Keys(ctx context.Context, pattern string) ([]string, error)

Keys returns all keys matching the glob pattern.

func (*MemoryCache) MGet

func (c *MemoryCache) MGet(ctx context.Context, keys ...string) ([][]byte, error)

MGet retrieves multiple values from the cache.

func (*MemoryCache) MSet

func (c *MemoryCache) MSet(ctx context.Context, items map[string][]byte, ttl time.Duration) error

MSet stores multiple values in the cache.

func (*MemoryCache) Set

func (c *MemoryCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set stores a value in the cache with the given TTL.

func (*MemoryCache) SetJSON

func (c *MemoryCache) SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error

SetJSON marshals and stores a value as JSON in the cache.

func (*MemoryCache) Stats

func (c *MemoryCache) Stats() Stats

Stats returns cache statistics.

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) Handler

func (m *Middleware) Handler(ttl time.Duration) func(http.Handler) http.Handler

Handler returns a middleware handler that caches GET responses.

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) Close

func (c *RedisCache) Close() error

Close closes the Redis connection.

func (*RedisCache) Decr

func (c *RedisCache) Decr(ctx context.Context, key string) (int64, error)

Decr decrements a key's value.

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) Exists

func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists in the cache.

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value from the cache.

func (*RedisCache) GetJSON

func (c *RedisCache) GetJSON(ctx context.Context, key string, dest any) error

GetJSON retrieves and unmarshals a JSON value from the cache.

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.

func (*RedisCache) Incr

func (c *RedisCache) Incr(ctx context.Context, key string) (int64, error)

Incr increments a key's value.

func (*RedisCache) Keys

func (c *RedisCache) Keys(ctx context.Context, pattern string) ([]string, error)

Keys returns all keys matching the pattern.

func (*RedisCache) MGet

func (c *RedisCache) MGet(ctx context.Context, keys ...string) ([][]byte, error)

MGet retrieves multiple values from the cache.

func (*RedisCache) MSet

func (c *RedisCache) MSet(ctx context.Context, items map[string][]byte, ttl time.Duration) error

MSet stores multiple values in the cache.

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set stores a value in the cache with the given TTL.

func (*RedisCache) SetJSON

func (c *RedisCache) SetJSON(ctx context.Context, key string, value any, ttl time.Duration) error

SetJSON marshals and stores a value as JSON in the cache.

func (*RedisCache) Stats

func (c *RedisCache) Stats() Stats

Stats returns cache statistics.

type Stats

type Stats struct {
	Hits       int64
	Misses     int64
	Keys       int64
	MemoryUsed int64
}

Stats holds cache statistics.

Jump to

Keyboard shortcuts

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