cache

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache provides in-memory implementations of the core cache.Cache interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMemoryCache

func NewMemoryCache(opts Options) cache.Cache

NewMemoryCache creates an in-memory cache. If opts.CleanupInterval is positive, a background goroutine periodically removes expired entries; it is stopped by Close.

func NewRedisCache

func NewRedisCache(opts RedisOptions) cache.Cache

NewRedisCache creates a Redis-backed cache. Close releases the underlying client connections.

Types

type EvictionPolicy

type EvictionPolicy string

EvictionPolicy selects which entry to remove when the cache is full.

const (
	// EvictOldest removes the entry inserted least recently (by insertion time).
	EvictOldest EvictionPolicy = "oldest"
	// EvictLRU removes the entry accessed least recently.
	EvictLRU EvictionPolicy = "lru"
)

type Options

type Options struct {
	// DefaultTTL is applied to entries stored without an explicit TTL.
	// A non-positive value means entries never expire by default.
	DefaultTTL time.Duration
	// MaxSize bounds the number of entries. 0 means unbounded.
	MaxSize int
	// MaxBytes bounds the total size of stored data in bytes. 0 means unbounded.
	MaxBytes int64
	// CleanupInterval sets the period for the background expiry sweep.
	// A non-positive value disables the background goroutine.
	CleanupInterval time.Duration
	// Eviction is the policy used when MaxSize or MaxBytes is exceeded.
	// Defaults to EvictOldest.
	Eviction EvictionPolicy
	// Logger is used for debug logging. Optional.
	Logger logger.Logger
}

Options configures a memory cache.

type RedisOptions

type RedisOptions struct {
	// Addr is the Redis server address (host:port).
	Addr string
	// DB is the Redis database number.
	DB int
	// Username for Redis authentication. Optional.
	Username string
	// Password for Redis authentication. Optional.
	Password string
	// DefaultTTL is applied to entries stored without an explicit TTL.
	// A non-positive value means entries never expire by default.
	DefaultTTL time.Duration
	// KeyPrefix is prepended to all cache keys. Defaults to "tnet:cache:".
	KeyPrefix string
	// Logger is used for debug logging. Optional.
	Logger logger.Logger
}

RedisOptions configures a Redis cache.

Jump to

Keyboard shortcuts

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