Documentation
¶
Overview ¶
Package cache defines generic interfaces for caching operations. It provides standard contracts for key-value caches, allowing different implementations (e.g., in-memory, distributed) to be used interchangeably.
Overview ¶
The core of this package is the `Cache[K comparable, V any]` interface, supporting basic Get, Set, Delete, Len, and Clear operations. Keys must be comparable, and values can be of any type. All operations accept a `context.Context` for cancellation and deadline propagation.
For caches supporting time-based expiration, the `TTLCache[K, V]` interface extends `Cache[K, V]` with a `SetWithTTL` method.
Implementations ¶
This package defines interfaces; concrete implementations are provided in sub-packages. See the `inmem` sub-package for a thread-safe, configurable in-memory cache implementation supporting TTL, size limits, and background garbage collection.
Errors ¶
The package defines standard errors that implementations can return:
- ErrCacheMiss: The requested key was not found.
- ErrExpired: The requested key was found but has passed its expiration time.
- ErrCacheFull: The cache cannot store a new item because it reached its size limit.
- ErrCacheNotStopped: Returned by operations if the cache was not properly stopped (usage may vary by implementation).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewErrCacheFull ¶
func NewErrCacheFull() error
NewErrCacheFull creates a new instance of ErrCacheFull.
func NewErrCacheMiss ¶
func NewErrCacheMiss() error
NewErrCacheMiss creates a new instance of ErrCacheMiss.
func NewErrCacheNotStopped ¶
func NewErrCacheNotStopped() error
NewErrCacheNotStopped creates a new instance of ErrCacheNotStopped.
Types ¶
type Cache ¶
type Cache[K comparable, V any] interface { // Get retrieves a value from the cache by its key. // The context can be used to control the operation, such as // setting a timeout or cancellation. Get(ctx context.Context, key K) (value V, err error) // Set stores a value in the cache with the specified key. // The context can be used to control the operation, such as // setting a timeout or cancellation. Set(ctx context.Context, key K, value V) error // Delete removes a value from the cache by its key. // The context can be used to control the operation, such as // setting a timeout or cancellation. Delete(ctx context.Context, key K) error // Len returns the number of items in the cache. Len() int // Clear removes all items from the cache. // The context can be used to control the operation, such as // setting a timeout or cancellation. Clear(ctx context.Context) error // Stop stops the cache and releases any resources it holds. // The context can be used to control the operation, such as // setting a timeout or cancellation. Stop(ctx context.Context) error }
Cache is an interface that defines the basic operations for a cache. It allows for storing, retrieving, and deleting items in the cache. The cache is generic and can work with any key-value pair. The key type must be comparable, and the value type can be any type.
type ErrCacheFull ¶
type ErrCacheFull struct{}
ErrCacheFull is an error that indicates that the cache is full and cannot accept new items.
func (*ErrCacheFull) Code ¶
func (e *ErrCacheFull) Code() int
Code returns the error code for ErrCacheFull.
func (*ErrCacheFull) Error ¶
func (e *ErrCacheFull) Error() string
Error implements the error interface for ErrCacheFull.
func (*ErrCacheFull) Unwrap ¶
func (e *ErrCacheFull) Unwrap() error
Unwrap implements the Unwrap method for ErrCacheFull.
type ErrCacheMiss ¶
type ErrCacheMiss struct{}
ErrCacheMiss is an error that indicates that a requested item was not found in the cache.
func (*ErrCacheMiss) Code ¶
func (e *ErrCacheMiss) Code() int
Code returns the error code for ErrCacheMiss.
func (*ErrCacheMiss) Error ¶
func (e *ErrCacheMiss) Error() string
Error implements the error interface for ErrCacheMiss.
func (*ErrCacheMiss) Unwrap ¶
func (e *ErrCacheMiss) Unwrap() error
Unwrap implements the Unwrap method for ErrCacheMiss.
type ErrCacheNotStopped ¶
type ErrCacheNotStopped struct{}
ErrCacheNotStopped is an error that indicates that the cache has not been stopped properly.
func (*ErrCacheNotStopped) Code ¶
func (e *ErrCacheNotStopped) Code() int
Code returns the error code for ErrCacheNotStopped.
func (*ErrCacheNotStopped) Error ¶
func (e *ErrCacheNotStopped) Error() string
Error implements the error interface for ErrCacheNotStopped.
func (*ErrCacheNotStopped) Unwrap ¶
func (e *ErrCacheNotStopped) Unwrap() error
Unwrap implements the Unwrap method for ErrCacheNotStopped.
type ErrExpired ¶
type ErrExpired struct{}
ErrExpired is an error that indicates that a requested item has expired in the cache.
func (*ErrExpired) Code ¶
func (e *ErrExpired) Code() int
Code returns the error code for ErrExpired.
func (*ErrExpired) Error ¶
func (e *ErrExpired) Error() string
Error implements the error interface for ErrExpired.
func (*ErrExpired) Unwrap ¶
func (e *ErrExpired) Unwrap() error
Unwrap implements the Unwrap method for ErrExpired.
type TTLCache ¶
type TTLCache[K comparable, V any] interface { // Cache defines the basic operations for a cache. // It is embedded to provide the basic cache functionality. Cache[K, V] // SetWithTTL stores a value in the cache with the specified key // and a time-to-live (TTL) duration. // The context can be used to control the operation, such as // setting a timeout or cancellation. SetWithTTL(ctx context.Context, key K, value V, ttl time.Duration) error }
TTLCache is an interface that extends the Cache interface to support time-to-live (TTL) functionality. It provides a method to set a value with a specific TTL.