Documentation
¶
Index ¶
- type Cache
- func (c *Cache) Clear(_ context.Context)
- func (c *Cache) Close() error
- func (c *Cache) Delete(_ context.Context, key string)
- func (c *Cache) Get(_ context.Context, key string) (any, bool)
- func (c *Cache) Set(ctx context.Context, key string, value any)
- func (c *Cache) SetWithTTL(_ context.Context, key string, value any, ttl time.Duration)
- func (c *Cache) Size() int64
- type Config
- type Interface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a thread-safe in-memory cache with TTL and memory management.
func NewDefault ¶
func NewDefault() *Cache
NewDefault creates a new memory cache with default configuration.
func (*Cache) SetWithTTL ¶
SetWithTTL adds a value to the cache with a custom TTL.
type Config ¶
type Config struct {
// DefaultTTL is the default time-to-live for cache entries.
DefaultTTL time.Duration
// CleanupInterval is how often the cache runs cleanup.
CleanupInterval time.Duration
// MaxItems is the maximum number of items allowed in the cache.
MaxItems int
// OnEviction is called when an item is evicted from the cache.
OnEviction func(key string, value any)
}
Config contains options for configuring a cache.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a default configuration for the cache.
type Interface ¶
type Interface interface {
// Set adds a value to the cache with the default TTL.
Set(ctx context.Context, key string, value any)
// SetWithTTL adds a value to the cache with a custom TTL.
SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration)
// Get retrieves a value from the cache.
Get(ctx context.Context, key string) (any, bool)
// Delete removes a value from the cache.
Delete(ctx context.Context, key string)
// Clear removes all values from the cache.
Clear(ctx context.Context)
// Size returns the number of items in the cache.
Size() int64
// Close stops all background tasks and releases resources.
Close() error
}
Interface defines the operations a cache must support.
Click to show internal directories.
Click to hide internal directories.