Documentation
¶
Index ¶
- type Cache
- func (c *Cache[K, V]) Add(key K, value V) (oldValue V, ok bool)
- func (c *Cache[K, V]) Cap() int
- func (c *Cache[K, V]) Delete(key K) (value V, ok bool)
- func (c *Cache[K, V]) Get(key K) (value V, ok bool)
- func (c *Cache[K, V]) Has(key K) bool
- func (c *Cache[K, V]) Init(size int)
- func (c *Cache[K, V]) Items() func(yield func(K, V) bool)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Reset()
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 stores a finite number of key value pairs where keys are unique. Adding a new key value pair in a full cache result in overriding an existing key value pair using the second chance algorithm which yields efficiency similar to lru.
func New ¶
func New[K comparable, V any](size int) *Cache[K, V]
New instantiate a new cache with key of type K and value of type V. A key is unique and adding an existing key with a different value overrides the Size is the maximum number of
func (*Cache[K, V]) Add ¶
Add adds the key value pair to the cache. It returns false and the default value for the type when the pair could be inserted in a free slot. Otherwise it returns true and the overwritten or discarded value which may be recycled.
func (*Cache[K, V]) Delete ¶
Delete returns the deleted value and true, when key is found in the cache to allow recycling the value. Otherwise, it returns the default value and false.
func (*Cache[K, V]) Get ¶
Get returns the value associated to the given key and true when it is found in the cache. Otherwise it returns false and the default value for the value type.
func (*Cache[K, V]) Has ¶
Get returns the value associated to the given key and true when it is found in the cache. Otherwise it returns false and the default value for the value type.