Documentation
¶
Overview ¶
Package cache provides functionality for caching frequently accessed data.
This package implements a simple in-memory cache with expiration.
Index ¶
- func WithCache[T any](ctx context.Context, cache *Cache[T], key string, ...) (T, error)
- func WithCacheTTL[T any](ctx context.Context, cache *Cache[T], key string, ttl time.Duration, ...) (T, error)
- type Cache
- func (c *Cache[T]) Clear(ctx context.Context)
- func (c *Cache[T]) Delete(ctx context.Context, key string)
- func (c *Cache[T]) Get(ctx context.Context, key string) (T, bool)
- func (c *Cache[T]) Set(ctx context.Context, key string, value T)
- func (c *Cache[T]) SetWithTTL(ctx context.Context, key string, value T, ttl time.Duration)
- func (c *Cache[T]) Shutdown()
- func (c *Cache[T]) Size() int
- type Config
- type EvictionStrategy
- type Item
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache is a simple in-memory cache with expiration
func (*Cache[T]) SetWithTTL ¶
SetWithTTL adds an item to the cache with a custom expiration time
type Config ¶
type Config struct {
// Enabled determines if the cache is enabled
Enabled bool
// TTL is the default time-to-live for cache items
TTL time.Duration
// MaxSize is the maximum number of items in the cache
MaxSize int
// PurgeInterval is the interval at which expired items are purged
PurgeInterval time.Duration
}
Config contains cache configuration parameters
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a default cache configuration
func (Config) WithEnabled ¶
WithEnabled sets whether the cache is enabled
func (Config) WithMaxSize ¶
WithMaxSize sets the maximum number of items in the cache
func (Config) WithPurgeInterval ¶
WithPurgeInterval sets the interval at which expired items are purged
type EvictionStrategy ¶
type EvictionStrategy int
EvictionStrategy defines the strategy for evicting items when the cache is full
const ( // LRU evicts the least recently used item LRU EvictionStrategy = iota // LFU evicts the least frequently used item LFU // FIFO evicts the first item added to the cache FIFO // Random evicts a random item Random )
type Options ¶
type Options struct {
// Logger is used for logging cache operations
Logger *logging.ContextLogger
// Tracer is used for tracing cache operations
Tracer telemetry.Tracer
// Name is the name of the cache
Name string
}
Options contains additional options for the cache
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns default options for cache operations
func (Options) WithLogger ¶
func (o Options) WithLogger(logger *logging.ContextLogger) Options
WithLogger sets the logger for the cache