Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed May 21, 2022
1 parent 8b21131 commit 9f1dcb0
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,68 @@
[![Go Report](https://goreportcard.com/badge/github.com/qianbin/directcache)](https://goreportcard.com/report/github.com/qianbin/directcache)


A high performance GC-free cache library for Go-lang.
The high performance GC-free cache library for Golang.

### Features

- high performance
- no GC overhead
- LRU & custom eviction policy
- zero-copy access
- small per-entry space overhead
- Fast set/get, scales well with the number of goroutines
- High hit rate, benefit from LRU & custom eviction policies
- GC friendly, almost zero GC overhead
- Zero-copy access

### Installation

It requires Go 1.12 or newer.

```bash
go get github.com/qianbin/directcache
```

### Usage

```go
// capacity will be set to directcache.MinCapacity
c := directcache.New(0)

key := []byte("DirectCache")
val := []byte("is awesome,")

c.Set(key, val)
got, ok := c.Get(key)

fmt.Println(string(key), string(got), ok)
// Output: DirectCache is awesome, true
```

zero-copy access

```go
c.AdvGet(
key,
func(val []byte){
// consume the value
},
true, // peek, which means the recently-used flag won't be added to the accessed entry.
)
```

custom eviction policy
```go
shouldEvict := func(entry directcache.Entry) bool {
if entry.RecentlyUsed() {
return false
}
// custom rules...
return true
}

c.SetEvictionPolicy(shouldEvict)
```

### Benchmarks

The performance is compared with [FreeCache](https://github.com/coocood/freecache), [FastCache](https://github.com/VictoriaMetrics/fastcache) and [BigCache](https://github.com/allegro/bigcache). The code of benchmarks can be found under [./benches/](./benches/).

```bash
$ cd benches
$ go test -run=^$ -bench ^Benchmark benches -benchtime=5s
Expand All @@ -28,18 +78,22 @@ BenchmarkGet/DirectCache-8 37590464 158.7 ns/op 151.22 MB/
BenchmarkGet/FreeCache-8 25786171 230.9 ns/op 103.96 MB/s
BenchmarkGet/FastCache-8 37254318 159.3 ns/op 150.64 MB/s
BenchmarkGet/BigCache-8 37842639 186.1 ns/op 128.97 MB/s

BenchmarkParallelGet/DirectCache-8 161808373 36.99 ns/op 648.87 MB/s
BenchmarkParallelGet/FreeCache-8 100000000 53.11 ns/op 451.90 MB/s
BenchmarkParallelGet/FastCache-8 179409560 33.35 ns/op 719.61 MB/s
BenchmarkParallelGet/BigCache-8 134716734 44.69 ns/op 536.99 MB/s

BenchmarkSet/DirectCache-8 25885528 371.9 ns/op 64.54 MB/s
BenchmarkSet/FreeCache-8 26270774 335.2 ns/op 71.60 MB/s
BenchmarkSet/FastCache-8 28288302 225.2 ns/op 106.58 MB/s
BenchmarkSet/BigCache-8 19396027 462.2 ns/op 51.93 MB/s

BenchmarkParallelSet/DirectCache-8 84369208 85.00 ns/op 282.34 MB/s
BenchmarkParallelSet/FreeCache-8 73204508 86.74 ns/op 276.68 MB/s
BenchmarkParallelSet/FastCache-8 82061668 86.49 ns/op 277.50 MB/s
BenchmarkParallelSet/BigCache-8 71246137 96.85 ns/op 247.81 MB/s

BenchmarkParallelSetGet/DirectCache-8 32410581 215.7 ns/op 111.26 MB/s
BenchmarkParallelSetGet/FreeCache-8 23206942 293.7 ns/op 81.71 MB/s
BenchmarkParallelSetGet/FastCache-8 32624253 179.3 ns/op 133.87 MB/s
Expand Down Expand Up @@ -71,55 +125,6 @@ PASS
ok benches 3.439s
```

### Installation

It requires Go 1.12 or newer.

```bash
go get github.com/qianbin/directcache
```

### Usage

```go
// capacity will be set to directcache.MinCapacity
c := directcache.New(0)

key := []byte("DirectCache")
val := []byte("is awesome,")

c.Set(key, val)
got, ok := c.Get(key)

fmt.Println(string(key), string(got), ok)
// Output: DirectCache is awesome, true
```

zero-copy access

```go
c.AdvGet(
key,
func(val []byte){
// consume the value
},
true, // peek, which means the recently-used flag won't be added to the accessed entry.
)
```

custom eviction policy
```go
shouldEvict := func(entry directcache.Entry) bool {
if entry.RecentlyUsed() {
return false
}
// custom rules...
return true
}

c.SetEvictionPolicy(shouldEvict)
```

## License

The MIT License

0 comments on commit 9f1dcb0

Please sign in to comment.