MCPcopy Index your code
hub / github.com/hashicorp/golang-lru

github.com/hashicorp/golang-lru @v2.0.7 sqlite

repository ↗ · DeepWiki ↗ · release v2.0.7 ↗
170 symbols 685 edges 14 files 125 documented · 74% 127 cross-repo links
README

golang-lru

This provides the lru package which implements a fixed-size thread safe LRU cache. It is based on the cache in Groupcache.

Documentation

Full docs are available on Go Packages

LRU cache example

package main

import (
    "fmt"
    "github.com/hashicorp/golang-lru/v2"
)

func main() {
    l, _ := lru.New[int, any](128)
    for i := 0; i < 256; i++ {
        l.Add(i, nil)
    }
    if l.Len() != 128 {
        panic(fmt.Sprintf("bad len: %v", l.Len()))
    }
}

Expirable LRU cache example

package main

import (
    "fmt"
    "time"

    "github.com/hashicorp/golang-lru/v2/expirable"
)

func main() {
    // make cache with 10ms TTL and 5 max keys
    cache := expirable.NewLRU[string, string](5, nil, time.Millisecond*10)


    // set value under key1.
    cache.Add("key1", "val1")

    // get value under key1
    r, ok := cache.Get("key1")

    // check for OK value
    if ok {
        fmt.Printf("value before expiration is found: %v, value: %q\n", ok, r)
    }

    // wait for cache to expire
    time.Sleep(time.Millisecond * 12)

    // get value under key1 after key expiration
    r, ok = cache.Get("key1")
    fmt.Printf("value after expiration is found: %v, value: %q\n", ok, r)

    // set value under key2, would evict old entry because it is already expired.
    cache.Add("key2", "val2")

    fmt.Printf("Cache len: %d\n", cache.Len())
    // Output:
    // value before expiration is found: true, value: "val1"
    // value after expiration is found: false, value: ""
    // Cache len: 1
}

Extension points exported contracts — how you extend this code

EvictCallback (FuncType)
EvictCallback is used to get a callback when a cache entry is evicted
expirable/expirable_lru.go
EvictCallback (FuncType)
EvictCallback is used to get a callback when a cache entry is evicted
simplelru/lru.go
LRUCache (Interface)
LRUCache is the interface for simple LRU cache.
simplelru/lru_interface.go

Core symbols most depended-on inside this repo

Add
called by 135
simplelru/lru_interface.go
Len
called by 123
simplelru/lru_interface.go
Get
called by 56
simplelru/lru_interface.go
Contains
called by 35
simplelru/lru_interface.go
Remove
called by 26
simplelru/lru_interface.go
Peek
called by 16
simplelru/lru_interface.go
NewLRU
called by 15
expirable/expirable_lru.go
NewLRU
called by 15
simplelru/lru.go

Shape

Method 95
Function 64
Struct 8
FuncType 2
Interface 1

Languages

Go100%

Modules by API surface

expirable/expirable_lru.go21 symbols
lru.go19 symbols
expirable/expirable_lru_test.go18 symbols
simplelru/lru.go17 symbols
internal/list.go15 symbols
2q.go14 symbols
simplelru/lru_interface.go13 symbols
arc/arc.go12 symbols
lru_test.go11 symbols
arc/arc_test.go11 symbols
2q_test.go10 symbols
simplelru/lru_test.go8 symbols

Dependencies from manifests, versioned

For agents

$ claude mcp add golang-lru \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact