| commit | 08ae0032eac7d0a0e8b118bef334f8e04aad8d80 | [log] [tgz] |
|---|---|---|
| author | KaushikiAnand <[email protected]> | Thu Feb 27 08:16:56 2025 |
| committer | GitHub <[email protected]> | Thu Feb 27 08:16:56 2025 |
| tree | 27729b8747865dd8df0d0e6944c9e8c0f4639798 | |
| parent | 5ae5436527f83e84f0b2b78e99ee277082ff9db2 [diff] | |
| parent | b9f15d4a24ad9c04fc8ae124db99a57044dbc9e8 [diff] |
Merge pull request #191 from hashicorp/build-test IND-2336 ci.yml updated with unit test coverage, .go-version added
This provides the lru package which implements a fixed-size thread safe LRU cache. It is based on the cache in Groupcache.
Full docs are available on Go Packages
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()))
}
}
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 }