Documentation
¶
Index ¶
- type Cache
- func (c *Cache[K, V]) Capacity() int
- func (c *Cache[K, V]) Clear()
- func (c *Cache[K, V]) Close()
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) DeleteByFunc(f func(key K, value V) bool)
- func (c *Cache[K, V]) Get(key K) (V, bool)
- func (c *Cache[K, V]) GetNode(key K) (node.Node[K, V], bool)
- func (c *Cache[K, V]) GetNodeQuietly(key K) (node.Node[K, V], bool)
- func (c *Cache[K, V]) Has(key K) bool
- func (c *Cache[K, V]) Range(f func(key K, value V) bool)
- func (c *Cache[K, V]) Set(key K, value V) bool
- func (c *Cache[K, V]) SetIfAbsent(key K, value V) bool
- func (c *Cache[K, V]) SetIfAbsentWithTTL(key K, value V, ttl time.Duration) bool
- func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration) bool
- func (c *Cache[K, V]) Size() int
- func (c *Cache[K, V]) Stats() *stats.Stats
- func (c *Cache[K, V]) WithExpiration() bool
- type Config
- type DeletionCause
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]) 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
DeleteByFunc deletes the association for this key from the cache when the given function returns true.
func (*Cache[K, V]) GetNode ¶ added in v1.2.0
GetNode returns the node associated with the key in this cache.
func (*Cache[K, V]) GetNodeQuietly ¶ added in v1.2.0
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]) Range ¶
Range iterates over all items in the cache.
Iteration stops early when the given function returns false.
func (*Cache[K, V]) Set ¶
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 ¶
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 ¶
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 ¶
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]) WithExpiration ¶ added in v1.2.0
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