A thread-safe capacity-constrained hash table that evicts key/value pairs according to the LRU (least recently used) policy. It is technically a thin wrapper around Go's built-in map type.
package main
import (
cmap "github.com/kchristidis/cmap
)
func main() {
cm, err := cmap.New(2) // will hold up to 2 key/value pairs
if err != nil {
log.Fatal(err)
}
cm.Put("fooKey", "fooVal")
v, ok := cm.Get("fooKey") // retrieve the value
fmt.Println(ok)
fmt.Println(v)
cm.Put("barKey", "barVal") // retrieve value as above
cm.Put("bazKey", "bazVal") // at this point "fooKey" is evicted
_, ok = cm.Get("fooKey")
fmt.Println(ok) // expected output: false
}
You may also want to consult the package documentation in GoDoc.
Contributions are welcome. Fork this library and submit a pull request.