Skip to content

AnthonyLonsMax/lrucache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lrucache

Generic thread-safe LRU (Least Recently Used) cache for Go.

Usage

import "github.com/AnthonyLonsMax/lrucache"

c := lrucache.New[string, int](100) // capacity

c.Put("a", 1)
c.Put("b", 2)

v, ok := c.Get("a")    // 1, true
c.Contains("a")        // true
c.Update("a", 99)
c.Delete("b")

// iterate
for k, v := range c.All() {
    fmt.Println(k, v)
}

When the cache exceeds capacity, the least recently used entry is evicted on insert.

Tip: Don't call mutating methods (Get, Put, Delete, etc.) inside an All() loop — it will deadlock. Collect keys first if you need to modify while iterating.

License

MIT

About

Generic thread-safe LRU cache for Go.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors