ttlcache

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package ttlcache provides a cache where items expire

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 caches values of V using keys of K. Cache operations are concurrency-safe

func New

func New[K comparable, V any](ctx context.Context, lifetime, cleanupInterval time.Duration) Cache[K, V]

New creates a new cache that invalidates items older than lifetime, pruning expired items every cleanupInterval. It launches a goroutine to perform this cleanup which exits when ctx is cancelled.

func (Cache[K, V]) Cleanup

func (c Cache[K, V]) Cleanup()

Cleanup removes expired items from the cache.

func (Cache[K, V]) Delete

func (c Cache[K, V]) Delete(k K)

Delete a value from the cache. Deleting a key that isn't present is safe

func (Cache[K, V]) Get

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

Get a value from cache. The bool return value indicates whether or not the key was present.

func (Cache[K, V]) Has

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

Has checks whether a value is in the cache

func (Cache[K, V]) Set

func (c Cache[K, V]) Set(k K, v V)

Set a value in the cache

Jump to

Keyboard shortcuts

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