Documentation
¶
Overview ¶
Example ¶
cache := NewCache[int, struct{}](8, func(key int, _ struct{}) {
fmt.Println("evicted", key)
})
for i := 1; i <= 10; i++ {
cache.Replace(i, struct{}{})
}
fmt.Printf("%d items in cache\n", cache.Len())
for k, v := range cache.IterateAsc() {
fmt.Println(k, v)
}
Output: evicted 1 evicted 2 8 items in cache 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 {}
Index ¶
- type Cache
- func (c *Cache[K, V]) Cap() int
- func (c *Cache[K, V]) Get(key K) (val V, ok bool)
- func (c *Cache[K, V]) GetOrSet(key K, setter func(K) (V, error)) (val V, err error)
- func (c *Cache[K, V]) Has(key K) (ok bool)
- func (c *Cache[K, V]) Iterate() iter.Seq2[K, V]
- func (c *Cache[K, V]) IterateAsc() iter.Seq2[K, V]
- func (c *Cache[K, V]) IterateDesc() iter.Seq2[K, V]
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Remove(key K) (existed bool)
- func (c *Cache[K, V]) RemoveAll()
- func (c *Cache[K, V]) Replace(key K, val V) (existed bool)
- func (c *Cache[K, V]) Reset()
- func (c *Cache[K, V]) Resize(capacity int)
- func (c *Cache[K, V]) Set(key K, val V) (ok bool)
Examples ¶
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 }
LRU cache. Not thread-safe.
func NewCache ¶
func NewCache[K comparable, V any](capacity int, evicted ...func(key K, val V)) *Cache[K, V]
func (*Cache[K, V]) IterateAsc ¶
Iterate all items in ascending order.
func (*Cache[K, V]) IterateDesc ¶
Iterate all items in descending order.
func (*Cache[K, V]) RemoveAll ¶
func (c *Cache[K, V]) RemoveAll()
Clear cache and notify each evict. To clear cache without notice, use Reset.
Click to show internal directories.
Click to hide internal directories.