ttlcache

package
v0.3.336 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package ttlcache provides an experimental passive, fixed-TTL LRU cache. It starts no goroutines. Expired entries are unreadable immediately, but can retain references until a subsequent write, Len, DeleteAll, or disposal.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache uses jellydator for TTL checks and read recency. The oldest write has the earliest deadline because Get never extends TTL. Keep write order independently so one Set can reclaim one expired slot, regardless of recency.

func New

func New[K comparable, V any](capacity int, ttl time.Duration) *Cache[K, V]

New creates a cache. Nonpositive capacity means unlimited and nonpositive TTL means no expiration.

func (*Cache[K, V]) Delete

func (c *Cache[K, V]) Delete(key K)

Delete removes a key, if present.

func (*Cache[K, V]) DeleteAll

func (c *Cache[K, V]) DeleteAll()

DeleteAll releases all retained entries.

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, bool)

Get returns a live value and promotes its LRU position without renewing TTL.

func (*Cache[K, V]) Has

func (c *Cache[K, V]) Has(key K) bool

Has rejects expired entries without updating recency or expiration.

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

Len reclaims expired entries and reports the live count.

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, value V)

Set renews expiration and reclaims at most one expired entry per write.

Jump to

Keyboard shortcuts

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