No description
Find a file
Sagar Verma 5015efc0ee
Merge pull request #214 from jaime-amate/feat/close-restart-goroutine
feat: close/restart cleanup goroutine in runtime
2026-02-24 08:55:29 +05:30
.github [chore] : Bump actions/setup-go from 6.1.0 to 6.2.0 in the actions group 2026-01-18 19:32:27 +00:00
arc [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
expirable unlock before return 2026-01-09 11:30:58 -08:00
internal move expirable LRU into separate package 2023-08-07 00:40:35 +02:00
simplelru [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
.gitignore Initial commit 2014-08-06 15:14:53 -07:00
.go-version go version back to 1.19 2025-06-10 15:20:33 +05:30
.golangci.yml [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
2q.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
2q_test.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
CHANGELOG.md IND-3870 enabling dependabot 2025-06-10 15:11:56 +05:30
doc.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
go.mod go version back to 1.19 2025-06-10 15:20:33 +05:30
LICENSE [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
lru.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
lru_test.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00
README.md delete expirable LRU Close method 2023-08-07 20:23:56 +02:00
testing_test.go [COMPLIANCE] Update Copyright and License Headers 2025-11-03 21:22:37 +00:00

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
}