Skip to content

A thread-safe capacity-constrained hash table that evicts keys according to the LRU policy

License

Notifications You must be signed in to change notification settings

kchristidis/cmap

Repository files navigation

cmap

DOI GoDoc Build Status

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.

Usage

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.

Contributing

Contributions are welcome. Fork this library and submit a pull request.

About

A thread-safe capacity-constrained hash table that evicts keys according to the LRU policy

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages