simple

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: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PutEntry added in v3.5.5

func PutEntry(e *Entry)

PutEntry replaces an Entry in the pool.

Types

type Cache

type Cache[Key comparable, Value any] struct {
	// 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]

	// Embedded mutex.
	sync.Mutex
}

Cache is the underlying Cache 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) *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)

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

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]) Swap

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

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

func (*Cache[K, V]) Trim added in v3.5.1

func (c *Cache[K, V]) Trim(perc float64)

Trim will truncate the cache to ensure it stays within given percentage of total capacity.

type Entry

type Entry struct {
	Key   any
	Value any
}

Entry represents an item in the cache.

func GetEntry added in v3.5.5

func GetEntry() *Entry

GetEntry fetches an Entry from pool, or allocates new.

Jump to

Keyboard shortcuts

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