ttl

package
v3.5.7 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[Key comparable, Value any] struct {
	// TTL is the cache item TTL.
	TTL time.Duration

	// Evict is the hook that is called when an item is evicted from the cache.
	Evict func(Key, Value)

	// Invalid is the hook that is called when an item's data in the cache is invalidated, includes Add/Set.
	Invalid func(Key, Value)

	// Cache is the underlying hashmap used for this cache.
	Cache maps.LRUMap[Key, *Entry[Key, Value]]

	// Embedded mutex.
	sync.Mutex
	// contains filtered or unexported fields
}

Cache is the underlying TTLCache implementation, providing both the base Cache interface and unsafe access to underlying map to allow flexibility in building your own.

func New

func New[K comparable, V any](len, cap int, ttl time.Duration) *Cache[K, V]

New returns a new initialized Cache with given initial length, maximum capacity and item TTL.

func (*Cache[K, V]) Add

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

Add: implements cache.Cache's Add().

func (*Cache[K, V]) CAS

func (c *Cache[K, V]) CAS(key K, old V, new V, cmp func(V, V) bool) bool

CAS: implements cache.Cache's CAS().

func (*Cache[K, V]) Cap

func (c *Cache[K, V]) Cap() (l int)

Cap: implements cache.Cache's Cap().

func (*Cache[K, V]) Clear

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

Clear: implements cache.Cache's Clear().

func (*Cache[K, V]) Get

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

Get: implements cache.Cache's Get().

func (*Cache[K, V]) Has

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

Has: implements cache.Cache's Has().

func (*Cache[K, V]) Init

func (c *Cache[K, V]) Init(len, cap int, ttl time.Duration)

Init will initialize this cache with given initial length, maximum capacity and item TTL.

func (*Cache[K, V]) Invalidate

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

Invalidate: implements cache.Cache's Invalidate().

func (*Cache[K, V]) InvalidateAll added in v3.3.1

func (c *Cache[K, V]) InvalidateAll(keys ...K) (ok bool)

InvalidateAll: implements cache.Cache's InvalidateAll().

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() (l int)

Len: implements cache.Cache's Len().

func (*Cache[K, V]) Set

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

Set: implements cache.Cache's Set().

func (*Cache[K, V]) SetEvictionCallback

func (c *Cache[K, V]) SetEvictionCallback(hook func(K, V))

SetEvictionCallback: implements cache.Cache's SetEvictionCallback().

func (*Cache[K, V]) SetInvalidateCallback

func (c *Cache[K, V]) SetInvalidateCallback(hook func(K, V))

SetInvalidateCallback: implements cache.Cache's SetInvalidateCallback().

func (*Cache[K, V]) SetTTL

func (c *Cache[K, V]) SetTTL(ttl time.Duration, update bool)

SetTTL: implements cache.Cache's SetTTL().

func (*Cache[K, V]) Start

func (c *Cache[K, V]) Start(freq time.Duration) (ok bool)

Start: implements cache.Cache's Start().

func (*Cache[K, V]) Stop

func (c *Cache[K, V]) Stop() (ok bool)

Stop: implements cache.Cache's Stop().

func (*Cache[K, V]) Swap

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

Swap: implements cache.Cache's Swap().

func (*Cache[K, V]) Sweep

func (c *Cache[K, V]) Sweep(_ time.Time)

Sweep attempts to evict expired items (with callback!) from cache.

type Entry

type Entry[Key comparable, Value any] struct {
	Key    Key
	Value  Value
	Expiry uint64
}

Entry represents an item in the cache, with it's currently calculated Expiry time.

Jump to

Keyboard shortcuts

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