https://github.com/floatdrop/2q
https://github.com/floatdrop/2q
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/floatdrop/2q
- Owner: floatdrop
- License: mit
- Created: 2022-03-27T13:27:26.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-24T10:47:47.000Z (about 2 years ago)
- Last Synced: 2025-03-14T01:31:40.875Z (about 2 months ago)
- Language: Go
- Size: 14.6 KB
- Stars: 42
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-go - 2q - 2Q in-memory cache implementation. (Database / Caches)
- awesome-go-extra - 2q - 03-27T13:27:26Z|2022-04-01T14:08:13Z| (Generators / Caches)
README
# 2q
[](https://pkg.go.dev/github.com/floatdrop/2q)
[](https://github.com/floatdrop/2q/actions/workflows/ci.yml)

[](https://goreportcard.com/report/github.com/floatdrop/2q)
[](https://github.com/avelino/awesome-go)Thread safe GoLang [2Q](http://www.vldb.org/conf/1994/P439.PDF) cache.
## Example
```go
import (
"fmt"twoqueue "github.com/floatdrop/2q"
)func main() {
cache := twoqueue.New[string, int](256)cache.Set("Hello", 5)
if e := cache.Get("Hello"); e != nil {
fmt.Println(*e)
// Output: 5
}
}
```## TTL
See [LRU TTL example](https://github.com/floatdrop/lru#ttl).
## Benchmarks
```
floatdrop/twoqueue:
Benchmark2Q_Rand-8 4384994 264.5 ns/op 46 B/op 3 allocs/op
Benchmark2Q_Freq-8 4862632 243.9 ns/op 44 B/op 3 allocs/ophashicorp/golang-lru:
Benchmark2Q_Rand-8 2847627 411.9 ns/op 135 B/op 5 allocs/op
Benchmark2Q_Freq-8 3323764 354.2 ns/op 122 B/op 5 allocs/op
```