mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-22 20:19:35 +01:00
2f33fe86f5
* Update dependencies and build to go1.22 * Fix api changes wrt to dependencies * Update golangci config |
||
---|---|---|
.. | ||
internal | ||
simplelru | ||
.gitignore | ||
.golangci.yml | ||
2q.go | ||
doc.go | ||
LICENSE | ||
lru.go | ||
README.md |
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() {
, _ := lru.New[int, any](128)
lfor i := 0; i < 256; i++ {
.Add(i, nil)
l}
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
:= expirable.NewLRU[string, string](5, nil, time.Millisecond*10)
cache
// set value under key1.
.Add("key1", "val1")
cache
// get value under key1
, ok := cache.Get("key1")
r
// check for OK value
if ok {
.Printf("value before expiration is found: %v, value: %q\n", ok, r)
fmt}
// wait for cache to expire
.Sleep(time.Millisecond * 12)
time
// get value under key1 after key expiration
, ok = cache.Get("key1")
r.Printf("value after expiration is found: %v, value: %q\n", ok, r)
fmt
// set value under key2, would evict old entry because it is already expired.
.Add("key2", "val2")
cache
.Printf("Cache len: %d\n", cache.Len())
fmt// Output:
// value before expiration is found: true, value: "val1"
// value after expiration is found: false, value: ""
// Cache len: 1
}