diff --git a/README.md b/README.md index c188667..13232e2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@
-
@@ -15,9 +15,200 @@ ## 📖 Contents +- [Motivation](#motivation) +- [Features](#features) +- [Usage](#usage) + - [Requirements](#requirements) + - [Installation](#installation) + - [Examples](#examples) +- [Benchmarks](#benchmarks) + - [Performance](#performance) + - [Hit ratio](#hit-ratio) - [Contribute](#contribute) - [License](#license) +## 💡 Motivation + +I once came across the fact that none of the Golang cache libraries are truly contention-free. All of them are just a standard map with mutex and some eviction policy. Unfortunately, these are not able to reach the speed of caches in other languages (such as Caffeine). For example, the fastest cache from Dgraph labs called [Ristretto](https://github.com/dgraph-io/ristretto), which was faster than competitors by 30% at best (Otter is many times faster) and had a [disgusting hit ratio](https://github.com/dgraph-io/ristretto/issues/336) even though README says otherwise. This can be a problem in different applications because no one wants to bump the performance of a cache library and its bad hit ratio 🙂. As a result, I wanted to get the fastest, easiest-to-use cache with excellent hit ratio and support from the authors and Otter is designed to correct this unfortunate misunderstanding. + +**Please leave a ⭐ as motivation if you liked the idea 😄** + +## ✨ Features + +This library has lots of features such as: +- **Simple API**: Just set the parameters you want in the builder and enjoy +- **Generics**: You can safely use comparable types as keys and any types as values +- **TTL**: Expired values will be automatically deleted from the cache +- **Excellent performance**: Otter is currently the fastest cache library with a huge lead over the [competition](#performance) +- **Great hit ratio**: New S3-FIFO algorithm is used, which shows excellent [results](#hit-ratio) + +## 📚 Usage + +### 📋 Requirements + +- Go 1.18+ + +### 🛠️ Installation + +```shell +go get -u github.com/maypok86/otter +``` + +### ✏️ Examples + +**Builder** + +Otter uses a builder pattern that allows you to conveniently create a cache object with different parameters + +```go +package main + +import ( + "github.com/maypok86/otter" +) + +func main() { + // NewBuilder creates a builder and sets the future cache capacity to 1000 elements. + // Returns an error if capacity <= 0. + builder, err := otter.NewBuilder[string, string](1000) + if err != nil { + panic(err) + } + + // ShardCount sets the number of cache shards to 256. + // The number of shards must always be a degree of two. + // Default is 128. + builder.ShardCount(256) + + // StatsEnabled determines whether statistics should be calculated when the cache is running. + // By default, statistics calculating is disabled. + builder.StatsEnabled(true) + + // Cost sets a function to dynamically calculate the weight of a key-value pair. + // By default this function always returns 1. + builder.Cost(func(key string, value string) uint32 { + return uint32(len(value)) + }) + + // Build creates a new cache object or + // returns an error if invalid parameters were passed to the builder. + cache, err := builder.Build() + if err != nil { + panic(err) + } + + cache.Close() +} +``` + +**Cache** +```go +package main + +import ( + "fmt" + "time" + + "github.com/maypok86/otter" +) + +func main() { + // create a cache with capacity equal to 10000 elements + cache, err := otter.MustBuilder[string, string](10_000).Build() + if err != nil { + panic(err) + } + + // set key-value pair with ttl (1 hour) + cache.SetWithTTL("key", "value", time.Hour) + + // get value from cache + value, ok := cache.Get("key") + if !ok { + panic("not found key") + } + fmt.Println(value) + + // delete key-value pair from cache + cache.Delete("key") + + // delete data and stop goroutines + cache.Close() +} +``` + +## 📊 Benchmarks + +The benchmark code can be found [here](https://github.com/maypok86/benchmarks) + +### 🚀 Performance + +#### Read (100%) + + + +#### Read (75%) / Write (25%) + + + +#### Read (50%) / Write (50%) + + + +#### Read (25%) / Write (75%) + + + +#### Write (100%) + + + +### 🎯 Hit ratio + +#### Zipf + + + +#### S3 + +This trace is described as "disk read accesses initiated by a large commercial search engine in response to various web search requests." + + + +#### DS1 + +This trace is described as "a database server running at a commercial site running an ERP application on top of a commercial database." + + + +#### P3 + +The trace P3 was collected from workstations running Windows NT by using Vtrace +which captures disk operations through the use of device +filters + + + +#### P8 + +The trace P8 was collected from workstations running Windows NT by using Vtrace +which captures disk operations through the use of device +filters + + + +#### LOOP + +This trace demonstrates a looping access pattern. + + + +#### OLTP + +This trace is described as "references to a CODASYL database for a one hour period." + + + ## 👏 Contribute Contributions are welcome as always, before submitting a new PR please make sure to open a new issue so community members can discuss it. diff --git a/assets/results/ds1.png b/assets/results/ds1.png new file mode 100644 index 0000000..0f8a2d7 Binary files /dev/null and b/assets/results/ds1.png differ diff --git a/assets/results/loop.png b/assets/results/loop.png new file mode 100644 index 0000000..252c3aa Binary files /dev/null and b/assets/results/loop.png differ diff --git a/assets/results/oltp.png b/assets/results/oltp.png new file mode 100644 index 0000000..2134794 Binary files /dev/null and b/assets/results/oltp.png differ diff --git a/assets/results/p3.png b/assets/results/p3.png new file mode 100644 index 0000000..a3c7067 Binary files /dev/null and b/assets/results/p3.png differ diff --git a/assets/results/p8.png b/assets/results/p8.png new file mode 100644 index 0000000..5e7ba59 Binary files /dev/null and b/assets/results/p8.png differ diff --git a/assets/results/reads=0,writes=100.png b/assets/results/reads=0,writes=100.png new file mode 100644 index 0000000..5b4e104 Binary files /dev/null and b/assets/results/reads=0,writes=100.png differ diff --git a/assets/results/reads=100,writes=0.png b/assets/results/reads=100,writes=0.png new file mode 100644 index 0000000..da710b2 Binary files /dev/null and b/assets/results/reads=100,writes=0.png differ diff --git a/assets/results/reads=25,writes=75.png b/assets/results/reads=25,writes=75.png new file mode 100644 index 0000000..6e3a2ad Binary files /dev/null and b/assets/results/reads=25,writes=75.png differ diff --git a/assets/results/reads=50,writes=50.png b/assets/results/reads=50,writes=50.png new file mode 100644 index 0000000..9a03f5f Binary files /dev/null and b/assets/results/reads=50,writes=50.png differ diff --git a/assets/results/reads=75,writes=25.png b/assets/results/reads=75,writes=25.png new file mode 100644 index 0000000..e3630e2 Binary files /dev/null and b/assets/results/reads=75,writes=25.png differ diff --git a/assets/results/s3.png b/assets/results/s3.png new file mode 100644 index 0000000..19592b2 Binary files /dev/null and b/assets/results/s3.png differ diff --git a/assets/results/zipf.png b/assets/results/zipf.png new file mode 100644 index 0000000..66a2916 Binary files /dev/null and b/assets/results/zipf.png differ