Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ammario/tlru
A generic TLRU cache for Go
https://github.com/ammario/tlru
Last synced: 25 days ago
JSON representation
A generic TLRU cache for Go
- Host: GitHub
- URL: https://github.com/ammario/tlru
- Owner: ammario
- License: cc0-1.0
- Created: 2021-12-24T23:22:54.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-03-11T18:40:24.000Z (8 months ago)
- Last Synced: 2024-06-21T14:41:01.462Z (5 months ago)
- Language: Go
- Homepage:
- Size: 2.71 MB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tlru
[![Go Reference](https://pkg.go.dev/badge/github.com/ammario/tlru.svg)](https://pkg.go.dev/github.com/ammario/tlru@master)Package `tlru` implements TLRU (Time Aware Least Recently Used)
cache for Go.Features:
* Uses generics for type-safety
* Memory-backed
* Safe for concurrent use
* No background threads```
go get github.com/ammario/tlru@master
```## Examples
Basic example:
```go
// This cache can store up to 100 values.
c := tlru.New[string](tlru.ConstantCost[int], 100)
c.Set("dog", 3.14, time.Second)// 3.14, ~time.Now().Add(time.Second), true
v, deadline, ok := c.Get("dog")
```Dynamic costs:
```go
// This cache can store up to 100 bytes.
c := New[string](
func(v string) int {
return len(v)
},
100,
)
c.Set("some_key", "some value", time.Minute)
```## Eviction
Cache eviction occurs during:
- Calls to `Set()`
- Calls to `Evict()`
- Calls to `Get()` (for that key only)Cache eviction is fast because the LRU and TTL indices are sorted. In most
cases, a call to the evictor only touches a few entries. Calling `Evict()`
directly is usually unnecessary.## Benchmarks
```
goos: darwin
goarch: amd64
pkg: github.com/ammario/tlru
cpu: VirtualApple @ 2.50GHz
Benchmark_TLRU_Get-10 7117467 141.9 ns/op
Benchmark_TLRU_Set-10 1222298 990.9 ns/op
```