cache

package
v0.25.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a thread-safe in-memory cache with TTL and memory management.

func New

func New(config Config) *Cache

New creates a new memory cache with the given configuration.

func NewDefault

func NewDefault() *Cache

NewDefault creates a new memory cache with default configuration.

func (*Cache) Clear

func (c *Cache) Clear(_ context.Context)

Clear removes all values from the cache.

func (*Cache) Close

func (c *Cache) Close() error

Close stops the cache cleanup goroutine.

func (*Cache) Delete

func (c *Cache) Delete(_ context.Context, key string)

Delete removes a value from the cache.

func (*Cache) Get

func (c *Cache) Get(_ context.Context, key string) (any, bool)

Get retrieves a value from the cache.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, value any)

Set adds a value to the cache with the default TTL.

func (*Cache) SetWithTTL

func (c *Cache) SetWithTTL(_ context.Context, key string, value any, ttl time.Duration)

SetWithTTL adds a value to the cache with a custom TTL.

func (*Cache) Size

func (c *Cache) Size() int64

Size returns the number of items in the cache.

type Config

type Config struct {
	// DefaultTTL is the default time-to-live for cache entries.
	DefaultTTL time.Duration

	// CleanupInterval is how often the cache runs cleanup.
	CleanupInterval time.Duration

	// MaxItems is the maximum number of items allowed in the cache.
	MaxItems int

	// OnEviction is called when an item is evicted from the cache.
	OnEviction func(key string, value any)
}

Config contains options for configuring a cache.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default configuration for the cache.

type Interface

type Interface interface {
	// Set adds a value to the cache with the default TTL.
	Set(ctx context.Context, key string, value any)

	// SetWithTTL adds a value to the cache with a custom TTL.
	SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration)

	// Get retrieves a value from the cache.
	Get(ctx context.Context, key string) (any, bool)

	// Delete removes a value from the cache.
	Delete(ctx context.Context, key string)

	// Clear removes all values from the cache.
	Clear(ctx context.Context)

	// Size returns the number of items in the cache.
	Size() int64

	// Close stops all background tasks and releases resources.
	Close() error
}

Interface defines the operations a cache must support.

Jump to

Keyboard shortcuts

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