cachettl

package
v0.46.2 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: AGPL-3.0 Imports: 2 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WithNoRefreshTTL = func(c *cacheConfig) {
	c.refreshTTL = false
}

WithNoRefreshTTL disables the refresh of the TTL when the cache is accessed.

View Source
var WithNow = func(now func() time.Time) Opt {
	return func(c *cacheConfig) {
		c.now = now
	}
}

WithNow sets the function to use to get the current time.

Functions

This section is empty.

Types

type Cache

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

Cache is a double linked list sorted by expiration time (ascending order) the root (head) node is the node with the lowest expiration time the tail node (end) is the node with the highest expiration time Cleanups are done on Get() calls so if Get() is never invoked then Nodes stay in-memory.

func New

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

New returns a new Cache.

func (*Cache[K, V]) Get

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

Get returns the value associated with the key or nil otherwise. Additionally, Get() will refresh the TTL by default and cleanup expired nodes.

func (*Cache[K, V]) OnEvicted added in v0.34.0

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

func (*Cache[K, V]) Put

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

Put adds or updates an element inside the Cache. The Cache will be sorted with the node with the highest expiration at the tail.

type Opt added in v0.42.1

type Opt func(*cacheConfig)

Jump to

Keyboard shortcuts

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