expcache

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package expcache provides an in-memory expiry cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache is an expiry cache that does not prune out of date elements.

Is thread-safe.

func New

func New[T any](f TimeToLiveFunc) *Cache[T]

Dynamic creates an expiry cache using a functional ttl computed based on the current time.

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, bool)

Get returns the value and whether the cache considers the value to be usable. The value is not usable if it does not exist, or is past its TTL expiration.

func (*Cache[T]) GetMap

func (c *Cache[T]) GetMap(keys ...string) map[string]Maybe[T]

GetMap returns a Maybe for each given key. The Maybe indicates whether the item is considered usable. An item is not usable if no value exists for a given key, or the value for the given key is past its TTL expiration.

func (*Cache[T]) Insert

func (c *Cache[T]) Insert(key string, item T)

Insert item into the cache at the given key.

func (*Cache[T]) InsertMap

func (c *Cache[T]) InsertMap(items map[string]T)

Insert each item of items into the cache at the associated key.

func (*Cache[T]) Touch

func (c *Cache[T]) Touch(key string)

Touch bumps the insertion time associated with key to be the current time, thus extending the effective TTL of the associated value.

type Maybe

type Maybe[T any] struct {
	Item   T
	Usable bool
}

type TimeToLiveFunc

type TimeToLiveFunc func() time.Duration

TimeToLiveFunc represents the function used by a Cache to determine how long an item should be accessible in the cache.

Often returns a static value, or a value based on time of day, etc.

func TTL

func TTL(duration time.Duration) TimeToLiveFunc

TTL returns a TimeToLiveFunc around a static ttl duration.

Jump to

Keyboard shortcuts

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