Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[TValue any] interface { // Get retrieves a value from the cache based on the provided key. It // returns the value and a boolean indicating whether the value was // found in the cache. Get(key TKey) (TValue, bool) // Put adds a new key-value pair to the cache. Put(key TKey, value TValue) // Delete removes a value from the cache based on the provided key. Delete(key TKey) }
Cache is a generic cache interface.
type DefaultCache ¶
type DefaultCache[TValue any] struct { // contains filtered or unexported fields }
DefaultCache is the default implementation of the Cache interface. It is not safe for concurrent use, as it meant to be embedded in code that does the concurrency control.
func NewCache ¶
func NewCache[TValue any](maxLength int) *DefaultCache[TValue]
NewCache creates a new cache based on the provided maximum length.
Setting the maxLength to -1 or less will disable the eviction of elements from the cache. A maxLength of 0 will create a pass-through cache that does nothing.
func (*DefaultCache[TValue]) Delete ¶
func (c *DefaultCache[TValue]) Delete(key TKey)
Delete removes a value from the cache based on the provided key.
func (*DefaultCache[TValue]) Get ¶
func (c *DefaultCache[TValue]) Get(key TKey) (TValue, bool)
Get retrieves a value from the cache based on the provided key. It returns the value and a boolean indicating whether the value was found in the cache.
func (*DefaultCache[TValue]) Hits ¶
func (c *DefaultCache[TValue]) Hits() int
Hits returns the number of cache hits (i.e. the number of Get calls that found the value in the cache).
func (*DefaultCache[TValue]) Misses ¶
func (c *DefaultCache[TValue]) Misses() int
Misses returns the number of cache misses (i.e. the number of Get calls that did not find the value in the cache).
func (*DefaultCache[TValue]) Put ¶
func (c *DefaultCache[TValue]) Put(key TKey, value TValue)
Put adds a new key-value pair to the cache.
func (*DefaultCache[TValue]) ResetRatio ¶
func (c *DefaultCache[TValue]) ResetRatio()
ResetRatio resets the ratio of hits to misses.