Documentation
¶
Overview ¶
Package cache provides caching functionality with support for Redis and in-memory caching. It offers a unified interface for different caching backends with TTL support.
Index ¶
- Variables
- type Cache
- type EvictionPolicy
- type GobSerializer
- type JSONSerializer
- type LoadFunc
- type LoadingCache
- func (lc *LoadingCache) Clear(ctx context.Context) error
- func (lc *LoadingCache) Delete(ctx context.Context, key string) error
- func (lc *LoadingCache) Exists(ctx context.Context, key string) (bool, error)
- func (lc *LoadingCache) Get(ctx context.Context, key string, value any) error
- func (lc *LoadingCache) Keys(ctx context.Context, pattern string) ([]string, error)
- func (lc *LoadingCache) Refresh(ctx context.Context, key string) error
- func (lc *LoadingCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (lc *LoadingCache) TTL(ctx context.Context, key string) (time.Duration, error)
- type MemoryCache
- func (c *MemoryCache) Clear(_ context.Context) error
- func (c *MemoryCache) Delete(_ context.Context, key string) error
- func (c *MemoryCache) Exists(_ context.Context, key string) (bool, error)
- func (c *MemoryCache) Get(_ context.Context, key string, value any) error
- func (c *MemoryCache) GetMultiple(_ context.Context, keys []string) (map[string]any, error)
- func (c *MemoryCache) Keys(_ context.Context, pattern string) ([]string, error)
- func (c *MemoryCache) Set(_ context.Context, key string, value any, ttl time.Duration) error
- func (c *MemoryCache) SetMultiple(_ context.Context, items map[string]any, ttl time.Duration) error
- func (c *MemoryCache) Stop()
- func (c *MemoryCache) TTL(_ context.Context, key string) (time.Duration, error)
- type MemoryCacheOption
- type Metrics
- type NamespaceCache
- func (nc *NamespaceCache) Clear(ctx context.Context) error
- func (nc *NamespaceCache) Delete(ctx context.Context, key string) error
- func (nc *NamespaceCache) Exists(ctx context.Context, key string) (bool, error)
- func (nc *NamespaceCache) Get(ctx context.Context, key string, value any) error
- func (nc *NamespaceCache) Keys(ctx context.Context, pattern string) ([]string, error)
- func (nc *NamespaceCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (nc *NamespaceCache) TTL(ctx context.Context, key string) (time.Duration, error)
- type Serializer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrKeyNotFound is returned when a key doesn't exist in the cache ErrKeyNotFound = errors.New("key not found") // ErrCacheMiss is an alias for ErrKeyNotFound for compatibility ErrCacheMiss = ErrKeyNotFound // ErrCacheFull is returned when the cache is at capacity ErrCacheFull = errors.New("cache is full") // ErrInvalidTTL is returned when an invalid TTL is provided ErrInvalidTTL = errors.New("invalid TTL") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a value from the cache
Get(ctx context.Context, key string, value any) error
// Set stores a value in the cache with optional TTL
Set(ctx context.Context, key string, value any, ttl time.Duration) error
// Delete removes a value from the cache
Delete(ctx context.Context, key string) error
// Exists checks if a key exists in the cache
Exists(ctx context.Context, key string) (bool, error)
// Clear removes all values from the cache
Clear(ctx context.Context) error
// TTL returns the remaining TTL for a key
TTL(ctx context.Context, key string) (time.Duration, error)
// Keys returns all keys matching a pattern (* for all)
Keys(ctx context.Context, pattern string) ([]string, error)
}
Cache defines the interface for cache implementations
type EvictionPolicy ¶
type EvictionPolicy string
EvictionPolicy defines cache eviction strategies
const ( // EvictionLRU removes least recently used items EvictionLRU EvictionPolicy = "lru" // EvictionLFU removes least frequently used items EvictionLFU EvictionPolicy = "lfu" // EvictionRandom removes random items EvictionRandom EvictionPolicy = "random" )
type GobSerializer ¶
type GobSerializer struct{}
GobSerializer serializes values using gob encoding
func (*GobSerializer) Deserialize ¶
func (gs *GobSerializer) Deserialize(data []byte, value any) error
Deserialize converts gob bytes to a value
type JSONSerializer ¶
type JSONSerializer struct{}
JSONSerializer serializes values as JSON
func (*JSONSerializer) Deserialize ¶
func (js *JSONSerializer) Deserialize(data []byte, value any) error
Deserialize converts JSON bytes to a value
type LoadingCache ¶
type LoadingCache struct {
// contains filtered or unexported fields
}
LoadingCache wraps a cache with automatic loading
func NewLoadingCache ¶
func NewLoadingCache(cache Cache, loadFunc LoadFunc) *LoadingCache
NewLoadingCache creates a new loading cache
func (*LoadingCache) Clear ¶
func (lc *LoadingCache) Clear(ctx context.Context) error
Clear removes all values from the cache
func (*LoadingCache) Delete ¶
func (lc *LoadingCache) Delete(ctx context.Context, key string) error
Delete removes a value from the cache
func (*LoadingCache) Refresh ¶
func (lc *LoadingCache) Refresh(ctx context.Context, key string) error
Refresh reloads a value in the cache
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is an in-memory cache implementation
func NewMemoryCache ¶
func NewMemoryCache(opts ...MemoryCacheOption) *MemoryCache
NewMemoryCache creates a new in-memory cache
func (*MemoryCache) Clear ¶
func (c *MemoryCache) Clear(_ context.Context) error
Clear removes all values from the cache
func (*MemoryCache) Delete ¶
func (c *MemoryCache) Delete(_ context.Context, key string) error
Delete removes a value from the cache
func (*MemoryCache) GetMultiple ¶
GetMultiple retrieves multiple values from the cache
func (*MemoryCache) SetMultiple ¶
SetMultiple stores multiple values in the cache
type MemoryCacheOption ¶
type MemoryCacheOption func(*MemoryCache)
MemoryCacheOption configures a MemoryCache
func WithCleanupInterval ¶
func WithCleanupInterval(interval time.Duration) MemoryCacheOption
WithCleanupInterval sets the interval for cleaning expired entries
func WithEvictionPolicy ¶
func WithEvictionPolicy(policy EvictionPolicy) MemoryCacheOption
WithEvictionPolicy sets the eviction policy
func WithMaxSize ¶
func WithMaxSize(size int) MemoryCacheOption
WithMaxSize sets the maximum number of entries
func WithMetrics ¶
func WithMetrics(metrics Metrics) MemoryCacheOption
WithMetrics sets the metrics collector
type Metrics ¶
type Metrics interface {
IncrHits(cache string)
IncrMisses(cache string)
IncrSets(cache string)
IncrDeletes(cache string)
IncrEvictions(cache string)
ObserveSize(cache string, size int)
}
Metrics defines the interface for cache metrics
type NamespaceCache ¶
type NamespaceCache struct {
// contains filtered or unexported fields
}
NamespaceCache wraps a cache with namespace support
func NewNamespaceCache ¶
func NewNamespaceCache(cache Cache, namespace string) *NamespaceCache
NewNamespaceCache creates a new namespace cache
func (*NamespaceCache) Clear ¶
func (nc *NamespaceCache) Clear(ctx context.Context) error
Clear removes all values from the namespace
func (*NamespaceCache) Delete ¶
func (nc *NamespaceCache) Delete(ctx context.Context, key string) error
Delete removes a value from the cache