cache

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: LGPL-3.0 Imports: 2 Imported by: 4

Documentation

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 manages items in a sync.Map, items are allowed to be permanent or have an expiry time. The keys which have an expiry time are stored in chain. chainAdd and chainDel are used to add/remove items to/from chain. These actions are completed in the cleaner goroutine to be concurrency safe.

func New

func New[K comparable, V any]() *Cache[K, V]

New creates a *Cache[K, V] ready to be used by the caller

func (*Cache[K, V]) Close

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

Close simply sends a signal to stop the cleaner goroutine. The cache is left in its current state and can still be read. The only difference being that items which have expired will not be garbage collected.

func (*Cache[K, V]) Delete

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

Delete removes an item from the cache.

func (*Cache[K, V]) Get

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

Get returns (value, true) for any existing key. Or (V{}, false) for any unknown key. This is equivalent to calling GetExpires and ignoring the expiry time return value.

func (*Cache[K, V]) GetExpires

func (c *Cache[K, V]) GetExpires(key K) (V, time.Time, bool)

GetExpires returns (value, expiry, true) for any existing key. Or (V{}, time.Time{}, false) for any unknown key.

func (*Cache[K, V]) Range

func (c *Cache[K, V]) Range(f func(key K, value V) bool)

Range calls f with every key-value pair, which has not expired, currently stored in the map.

If f returns false, range stops the iteration.

See sync/Map.Range for implementation specific details.

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, value V, expires time.Duration)

Set adds an item to the cache with an expiry duration.

If an item is added with the same key then this item with be overwritten.

func (*Cache[K, V]) SetAbs added in v0.0.8

func (c *Cache[K, V]) SetAbs(key K, value V, expires time.Time)

SetAbs adds an item to the cache with an expiry date.

If an item is added with the same key then this item with be overwritten.

func (*Cache[K, V]) SetPermanent

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

SetPermanent adds an item to the cache without an expiry date.

If an item is added with the same key then this item with be overwritten.

Jump to

Keyboard shortcuts

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