cache

package module
v0.0.0-...-6007e9e Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: BSD-3-Clause Imports: 6 Imported by: 3

Documentation

Overview

Cache works as thread-safe least recently used (LRU) cache with expiring items on a given TTL span. The TTL gets automatically cycled on each Get() call, which permits the item to stay longer in the cache. There is a soft cap by default on the cache items that get rotated out based on the expiry date.

Index

Constants

View Source
const (

	// Null disables the ttl
	Null time.Duration = 0
)

Variables

This section is empty.

Functions

func Debug

func Debug() bool

Debug toggles (enables/disables) debug output through Logger and returns either true if enabled or respectively false if disabled.

Types

type Cache

type Cache[Key comparable, Value any] struct {
	// Logger defaults to stderr and adds a prefix of "cache: "
	Logger *log.Logger
	// contains filtered or unexported fields
}

Cache holds all items and configuration.

func New

func New[Key comparable, Value any](ttl time.Duration, max uint) *Cache[Key, Value]

New creates a new cache instance, taking a ttl and max number for the containing items.

ttl = Null | never expires [~100 years]
max = 0    | fallback to default maxItems [64]

func (*Cache[Key, Value]) Count

func (c *Cache[Key, Value]) Count() uint

Count returns the number of items in the cache.

func (*Cache[Key, Value]) Delete

func (c *Cache[Key, Value]) Delete(key Key) bool

Delete removes the item called by the name of key from the cache.

func (*Cache[Key, Value]) EnableHardCap

func (c *Cache[Key, Value]) EnableHardCap()

EnableHardCap disables the ability to rotate the oldest item out of cache to be replaced by a new item.

func (*Cache[Key, Value]) Expire

func (c *Cache[Key, Value]) Expire(key Key, data Value) error

Expire adds a new item to the cache that expires after the TTL is reached, independent of Get calls.

func (*Cache[Key, Value]) Get

func (c *Cache[Key, Value]) Get(key Key) (Value, bool)

Get lookup items by key name and returns the data if found.

func (*Cache[Key, Value]) Info

func (c *Cache[Key, Value]) Info() []Info[Key]

Info returns metadata of the items in the cache.

func (*Cache[Key, Value]) Max

func (c *Cache[Key, Value]) Max() uint

Max returns the given max value from your New() call.

func (*Cache[Key, Value]) Once

func (c *Cache[Key, Value]) Once(key Key, data Value) error

Once adds new item to the cache that expires after the first cycle and gets no update on a Get call.

func (*Cache[Key, Value]) Persist

func (c *Cache[Key, Value]) Persist(key Key, data Value) error

Persist adds new item to the cache that never expires.

func (*Cache[Key, Value]) Set

func (c *Cache[Key, Value]) Set(key Key, data Value) error

Set adds new item to the cache that expires after the TTL is reached if it gets no Get calls to cycle.

func (*Cache[Key, Value]) Wipe

func (c *Cache[Key, Value]) Wipe() bool

Wipe removes all entries from the cache at once.

type Info

type Info[Key comparable] struct {
	Date time.Time
	Name Key
	Keep bool
	Once bool
}

Info holds metadata about the cache content.

Jump to

Keyboard shortcuts

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