Documentation
¶
Overview ¶
Package cache provides a high-performance, multi-layer caching library for Go. It combines an in-memory LRU cache for hot data with a Redis backend for a larger, distributed cache, offering a robust solution for reducing latency and database load.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("item not found in cache")
ErrNotFound is returned when a requested item is not found in any cache layer.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is the main cache controller. It orchestrates the flow of data between the in-memory LRU cache and the Redis cache.
func New ¶
New initializes a new multi-layer Cache with the given options. It returns an error if the configuration is invalid or if the connection to Redis fails.
func (*Cache) Get ¶
Get retrieves an item from the cache. It checks the in-memory LRU cache first, then the Redis cache. If the item is not found in either, it returns ErrNotFound.
type Options ¶
type Options struct { // RedisAddr is the address of the Redis server (e.g., "localhost:6379"). // This is ignored if RedisClient is provided. RedisAddr string // DefaultTTL is the default time-to-live for cache entries. DefaultTTL time.Duration // MaxMemEntries is the maximum number of entries to keep in the in-memory LRU cache. MaxMemEntries int // RedisClient allows providing an existing Redis client. If nil, a new client is created. RedisClient *redis.Client }
Options holds the configuration for creating a new Cache instance. It allows for customization of connection details, cache sizes, and expiration policies.