Documentation ¶
Index ¶
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 }
Cache is used a LRU (Least recently used) cache replacement policy.
Discards the least recently used items first. This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item.
func NewCache ¶
func NewCache[K comparable, V any](opts ...Option) *Cache[K, V]
NewCache creates a new non-thread safe LRU cache whose capacity is the default size (128).
Example ¶
package main import ( "fmt" "github.com/Code-Hex/go-generics-cache/policy/lru" ) func main() { c := lru.NewCache[string, int]() c.Set("a", 1) c.Set("b", 2) av, aok := c.Get("a") bv, bok := c.Get("b") cv, cok := c.Get("c") fmt.Println(av, aok) fmt.Println(bv, bok) fmt.Println(cv, cok) }
Output: 1 true 2 true 0 false
func (*Cache[K, V]) Delete ¶
func (c *Cache[K, V]) Delete(key K)
Delete deletes the item with provided key from the cache.
func (*Cache[K, V]) Keys ¶
func (c *Cache[K, V]) Keys() []K
Keys returns the keys of the cache. the order is from oldest to newest.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/go-generics-cache/policy/lru" ) func main() { c := lru.NewCache[string, int]() c.Set("a", 1) c.Set("b", 2) c.Set("c", 3) keys := c.Keys() for _, key := range keys { fmt.Println(key) } }
Output: a b c
Click to show internal directories.
Click to hide internal directories.