Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TTLCache

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

TTLCache is a simple in-memory cache with per-entry TTL expiry. Safe for concurrent use. Background goroutine evicts stale entries every 30s. Call Stop() to shut down the background goroutine.

func New

func New() *TTLCache

New creates a new TTLCache and starts the background eviction loop.

func NewBounded added in v0.5.0

func NewBounded(maxEntries int, maxBytes int64) *TTLCache

NewBounded creates a cache limited by entry count and retained bytes. Values admitted to a bounded cache must implement CacheSizeBytes. The byte charge includes that payload size, the key length, and a fixed per-entry charge for cache bookkeeping. It is an accounting bound, not an RSS bound.

func (*TTLCache) Delete

func (c *TTLCache) Delete(key string)

Delete removes a key immediately.

func (*TTLCache) Get

func (c *TTLCache) Get(key string) (any, bool)

Get returns the cached value and true if it exists and has not expired.

func (*TTLCache) Len added in v0.5.0

func (c *TTLCache) Len() int

Len reports the entries currently held, expired ones included until the eviction loop sweeps them.

func (*TTLCache) Set

func (c *TTLCache) Set(key string, value any, ttl time.Duration) bool

Set stores value under key with the given TTL. It returns false when a bounded cache cannot account for or admit the value.

func (*TTLCache) Stop

func (c *TTLCache) Stop()

Stop shuts down the background eviction goroutine and waits for it to exit.

func (*TTLCache) Usage added in v0.5.0

func (c *TTLCache) Usage() (entries int, chargedBytes int64)

Usage reports the current entry count and charged retained bytes. Unbounded caches report zero charged bytes.

Jump to

Keyboard shortcuts

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