cache

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2025 License: MIT Imports: 6 Imported by: 0

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

View Source
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

func New(opts Options) (*Cache, error)

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

func (c *Cache) Get(id string) ([]byte, error)

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.

func (*Cache) Purge

func (c *Cache) Purge(id string)

Purge removes an item from both cache layers.

func (*Cache) Set

func (c *Cache) Set(id string, value []byte)

Set stores an item in both cache layers with the default TTL.

func (*Cache) Stats

func (c *Cache) Stats() Stats

Stats returns statistics for the in-memory cache.

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.

type Stats

type Stats struct {
	MemHits   int
	MemMisses int
	MemEvicts int
	MemLen    int
	MemKeys   []interface{}
}

Stats contains statistics about the cache's performance.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL