From c667a5496602ea8eafd9da4a73f3611c0294fc75 Mon Sep 17 00:00:00 2001 From: phuslu Date: Tue, 2 Jan 2024 14:44:46 +0800 Subject: [PATCH] tweak readme --- README.md | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8c39e0d..44f3cf0 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ * Simple - No Dependency - - Less 500 lines of Go(excluding tests) + - Less 1000 lines of Go * Fast - Faster than all well-known **LRU** caches - Zero memory allocs @@ -15,10 +15,11 @@ - Continuous memory layout * Memory efficient - Uses only 24 extra bytes per cache object + - TTL (8 bytes) + ArrayList node (2 x 4 bytes) + Key hash (4 bytes) + Hash Entry index (4 bytes) ### Getting Started -try on https://go.dev/play/p/XhMxFJe8jcy +try on https://go.dev/play/p/f2653y1Kj6i ```go package main @@ -29,16 +30,24 @@ import ( ) func main() { - c := lru.New[string, int](1024) + cache1 := lru.New[string, int](1024) - c.SetWithTTL("a", 1, 1000*time.Millisecond) - println(c.Get("a")) + cache1.SetWithTTL("a", 1, 200*time.Millisecond) + println(cache1.Get("a")) - time.Sleep(500 * time.Millisecond) - println(c.Get("a")) + time.Sleep(100 * time.Millisecond) + println(cache1.Get("a")) - time.Sleep(1500 * time.Millisecond) - println(c.Get("a")) + time.Sleep(150 * time.Millisecond) + println(cache1.Get("a")) + + cache2 := lru.NewWithLoader[string, int](1024, func(string) (int, time.Duration, error) { + return 42, time.Hour, nil + }) + + println(cache2.Get("b")) + println(cache2.GetOrLoad("b", nil)) + println(cache2.Get("b")) } ``` @@ -50,19 +59,19 @@ goos: linux goarch: amd64 cpu: AMD EPYC 7763 64-Core Processor BenchmarkCloudflareGet -BenchmarkCloudflareGet-8 32122790 183.9 ns/op 18 B/op 1 allocs/op +BenchmarkCloudflareGet-8 34575751 171.1 ns/op 17 B/op 1 allocs/op BenchmarkEcacheGet -BenchmarkEcacheGet-8 49922084 120.0 ns/op 5 B/op 0 allocs/op +BenchmarkEcacheGet-8 52730455 114.2 ns/op 5 B/op 0 allocs/op BenchmarkRistrettoGet -BenchmarkRistrettoGet-8 33818906 195.6 ns/op 41 B/op 1 allocs/op +BenchmarkRistrettoGet-8 34192080 172.0 ns/op 41 B/op 1 allocs/op BenchmarkTheineGet -BenchmarkTheineGet-8 29022984 208.9 ns/op 0 B/op 0 allocs/op +BenchmarkTheineGet-8 29694378 202.5 ns/op 0 B/op 0 allocs/op BenchmarkOtterGet -BenchmarkOtterGet-8 71440144 85.35 ns/op 0 B/op 0 allocs/op +BenchmarkOtterGet-8 76333077 74.27 ns/op 0 B/op 0 allocs/op BenchmarkPhusluGet -BenchmarkPhusluGet-8 60555633 98.51 ns/op 0 B/op 0 allocs/op +BenchmarkPhusluGet-8 73868793 82.88 ns/op 0 B/op 0 allocs/op PASS -ok command-line-arguments 61.404s +ok command-line-arguments 46.827s ``` [godoc-img]: http://img.shields.io/badge/godoc-reference-blue.svg