https://github.com/mtchavez/lfu
LFU Cache in Go/Golang
https://github.com/mtchavez/lfu
cache go golang lfu-cache
Last synced: about 2 months ago
JSON representation
LFU Cache in Go/Golang
- Host: GitHub
- URL: https://github.com/mtchavez/lfu
- Owner: mtchavez
- License: mit
- Created: 2015-01-31T23:12:25.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-11-01T00:26:04.000Z (over 6 years ago)
- Last Synced: 2025-02-04T15:32:33.565Z (4 months ago)
- Topics: cache, go, golang, lfu-cache
- Language: Go
- Size: 27.3 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# LFU Cache
[](https://github.com/mtchavez/lfu/releases)
[](https://travis-ci.org/mtchavez/lfu)
[](http://godoc.org/github.com/mtchavez/lfu)
[](https://goreportcard.com/report/github.com/mtchavez/lfu)
[](https://codeclimate.com/github/mtchavez/lfu/maintainability)
[](https://codeclimate.com/github/mtchavez/lfu/test_coverage)LFU cache in Go offering O(1) Get and Insert [outlined here](http://dhruvbird.com/lfu.pdf).
## Install
`go get -u github.com/mtchavez/lfu`
## Usage
```go
package mainimport (
"fmt"
"github.com/mtchavez/lfu"
)func main() {
cache := lfu.NewLFU()// Insert
cache.Insert(42, []byte("user:42:[email protected]"))// Insert existing key
success, err := cache.Insert(42, "Nope")
if !success {
fmt.Println("Error inserting:", err)
}var data interface{}
var e error
// Get
data, e = cache.Get(42)
fmt.Println("Data for 42 is", data)// Get not found
data, e = cache.Get(987654321)
if e != nil {
fmt.Println("Error on get:", e)
}
}```
## Tests
`go test --cover`
## Benhcmarks
Get and insert methods are benchmarked. Results from OS X with
a 2.3 GHz Intel Core i7 on `go version go1.8.3 darwin/amd64````
# Updated: 2017-08-15BenchmarkInsert-8 1000000 1860 ns/op
BenchmarkParallelInsert-8 1000000 1861 ns/op
BenchmarkGet_EmptyCache-8 5000000 362 ns/op
BenchmarkGet_AllMisses-8 3000000 732 ns/op
BenchmarkGet_AllHits-8 1000000 1417 ns/op
BenchmarkParallelGet-8 2000000 1405 ns/op
```## TODO
* Some kind of eviction