core

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 12 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[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache is a structure performs a best-effort bounding of a hash table using eviction algorithm to determine which entries to evict when the capacity is exceeded.

func NewCache

func NewCache[K comparable, V any](c Config[K, V]) *Cache[K, V]

NewCache returns a new cache instance based on the settings from Config.

func (*Cache[K, V]) Capacity

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

Capacity returns the cache capacity.

func (*Cache[K, V]) Clear

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

Clear clears the hash table, all policies, buffers, etc.

NOTE: this operation must be performed when no requests are made to the cache otherwise the behavior is undefined.

func (*Cache[K, V]) Close

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

Close clears the hash table, all policies, buffers, etc and stop all goroutines.

NOTE: this operation must be performed when no requests are made to the cache otherwise the behavior is undefined.

func (*Cache[K, V]) Delete

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

Delete deletes the association for this key from the cache.

func (*Cache[K, V]) DeleteByFunc added in v1.1.0

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

DeleteByFunc deletes the association for this key from the cache when the given function returns true.

func (*Cache[K, V]) Get

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

Get returns the value associated with the key in this cache.

func (*Cache[K, V]) GetNode added in v1.2.0

func (c *Cache[K, V]) GetNode(key K) (node.Node[K, V], bool)

GetNode returns the node associated with the key in this cache.

func (*Cache[K, V]) GetNodeQuietly added in v1.2.0

func (c *Cache[K, V]) GetNodeQuietly(key K) (node.Node[K, V], bool)

GetNodeQuietly returns the node associated with the key in this cache.

Unlike GetNode, this function does not produce any side effects such as updating statistics or the eviction policy.

func (*Cache[K, V]) Has

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

Has checks if there is an item with the given key in the cache.

func (*Cache[K, V]) Range

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

Range iterates over all items in the cache.

Iteration stops early when the given function returns false.

func (*Cache[K, V]) Set

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

Set associates the value with the key in this cache.

If it returns false, then the key-value item had too much cost and the Set was dropped.

func (*Cache[K, V]) SetIfAbsent

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

SetIfAbsent if the specified key is not already associated with a value associates it with the given value.

If the specified key is not already associated with a value, then it returns false.

Also, it returns false if the key-value item had too much cost and the SetIfAbsent was dropped.

func (*Cache[K, V]) SetIfAbsentWithTTL

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

SetIfAbsentWithTTL if the specified key is not already associated with a value associates it with the given value and sets the custom ttl for this key-value item.

If the specified key is not already associated with a value, then it returns false.

Also, it returns false if the key-value item had too much cost and the SetIfAbsent was dropped.

func (*Cache[K, V]) SetWithTTL

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

SetWithTTL associates the value with the key in this cache and sets the custom ttl for this key-value item.

If it returns false, then the key-value item had too much cost and the SetWithTTL was dropped.

func (*Cache[K, V]) Size

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

Size returns the current number of items in the cache.

func (*Cache[K, V]) Stats

func (c *Cache[K, V]) Stats() *stats.Stats

Stats returns a current snapshot of this cache's cumulative statistics.

func (*Cache[K, V]) WithExpiration added in v1.2.0

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

WithExpiration returns true if the cache was configured with the expiration policy enabled.

type Config

type Config[K comparable, V any] struct {
	Capacity         int
	InitialCapacity  *int
	StatsEnabled     bool
	TTL              *time.Duration
	WithVariableTTL  bool
	CostFunc         func(key K, value V) uint32
	WithCost         bool
	DeletionListener func(key K, value V, cause DeletionCause)
}

Config is a set of cache settings.

type DeletionCause added in v1.2.0

type DeletionCause uint8

DeletionCause the cause why a cached entry was deleted.

const (
	// Explicit the entry was manually deleted by the user.
	Explicit DeletionCause = iota
	// Replaced the entry itself was not actually deleted, but its value was replaced by the user.
	Replaced
	// Size the entry was evicted due to size constraints.
	Size
	// Expired the entry's expiration timestamp has passed.
	Expired
)

func (DeletionCause) String added in v1.2.2

func (dc DeletionCause) String() string

Jump to

Keyboard shortcuts

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