localcache

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NoExpireDuration indicates that cache items should not expire.
	// Use this value to store items permanently in the cache.
	NoExpireDuration time.Duration = -1
)

Variables

This section is empty.

Functions

func New

func New[T any](opts ...Option) cache.Cache[T]

New creates a new localcache instance with optional configurations. It applies defaults if no options are provided and starts the cleanup goroutine if a cleanup interval is configured.

The cache is thread-safe and can be used concurrently from multiple goroutines. A background cleanup process will automatically remove expired items based on the configured cleanup interval.

Parameters:

  • opts: Optional configuration functions

Returns:

  • A new cache.Cache[T] instance

Example:

// Create cache with default settings
cache := New[string]()

// Create cache with custom expiration and cleanup interval
cache := New[User](
	WithDefaultExpiration(30 * time.Minute),
	WithCleanupInterval(5 * time.Minute),
)

Types

type Option

type Option func(*config)

Option is a function type for configuring the local cache.

func WithCleanupInterval

func WithCleanupInterval(interval time.Duration) Option

WithCleanupInterval sets the interval for automatically cleaning up expired items. A background goroutine will run periodically to remove expired items from memory.

func WithDefaultExpiration

func WithDefaultExpiration(expiration time.Duration) Option

WithDefaultExpiration sets the default expiration duration for cache items. If not specified, items will not expire by default.

Jump to

Keyboard shortcuts

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