From a8396e5c06d37de32262d0e284227fe9ff1c0ac0 Mon Sep 17 00:00:00 2001 From: Magnus Kokk Date: Fri, 9 Aug 2024 10:48:06 +0300 Subject: [PATCH] Update module and readme --- README.md | 50 ++++++++++++++++++------------ bench_test.go | 2 +- cache.go | 5 +-- cache_test.go | 2 +- doc.go | 2 +- example/main.go | 29 +++++++++++++++++ go.mod | 2 +- internal/backend/backend_test.go | 4 +-- internal/backend/benchmark_test.go | 2 +- internal/backend/element_test.go | 4 +-- options.go | 2 +- 11 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 example/main.go diff --git a/README.md b/README.md index d3a0d9e..fc63713 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,42 @@ # evcache -[![Go Reference](https://pkg.go.dev/badge/github.com/mgnsk/evcache/v3.svg)](https://pkg.go.dev/github.com/mgnsk/evcache/v3) -[![Go Report Card](https://goreportcard.com/badge/github.com/mgnsk/evcache/v3)](https://goreportcard.com/report/github.com/mgnsk/evcache/v3) +[![Go Reference](https://pkg.go.dev/badge/github.com/mgnsk/evcache/v4.svg)](https://pkg.go.dev/github.com/mgnsk/evcache/v4) +[![Go Report Card](https://goreportcard.com/badge/github.com/mgnsk/evcache/v4)](https://goreportcard.com/report/github.com/mgnsk/evcache/v4) Package evcache implements a concurrent key-value cache with capacity overflow eviction, item expiry and deduplication. -`import "github.com/mgnsk/evcache/v3"` - -Package evcache implements a concurrent key-value cache with capacity overflow eviction, item expiry and deduplication. +`import "github.com/mgnsk/evcache/v4"` ### Example ```go -c := evcache.New[string, string](10) - -// Fetches an existing value or calls the callback to get a new value. -result, err := c.Fetch("key", time.Minute, func() (string, error) { - // Possibly a very long network call. It only blocks write access to this key. - // Read access to the key is always non-blocking. - return "value", nil -}) - -if err != nil { - return err +package main + +import ( + "time" + + "github.com/mgnsk/evcache/v4" +) + +func main() { + c := evcache.New[string, string]( + evcache.WithCapacity(128), + evcache.WithPolicy(evcache.LRU), + evcache.WithTTL(time.Minute), + ) + + // Fetches an existing value or calls the callback to get a new value. + result, err := c.Fetch("key", func() (string, error) { + // Possibly a very long network call. It only blocks write access to this key. + // Read access for this key returns as if the value does not exist. + return "value", nil + }) + + if err != nil { + panic(err) + } + + // Use the result. + println(result) } - -// Use the result. -_ = result ``` diff --git a/bench_test.go b/bench_test.go index 9a5b2b2..d5c7551 100644 --- a/bench_test.go +++ b/bench_test.go @@ -5,7 +5,7 @@ import ( "sync/atomic" "testing" - "github.com/mgnsk/evcache/v3" + "github.com/mgnsk/evcache/v4" ) func BenchmarkFetchAndEvictParallel(b *testing.B) { diff --git a/cache.go b/cache.go index 1e8cbc1..f762562 100644 --- a/cache.go +++ b/cache.go @@ -1,13 +1,10 @@ -/* -Package evcache implements a concurrent key-value cache with capacity overflow eviction, item expiry and deduplication. -*/ package evcache import ( "runtime" "time" - "github.com/mgnsk/evcache/v3/internal/backend" + "github.com/mgnsk/evcache/v4/internal/backend" ) // Cache is an in-memory cache. diff --git a/cache_test.go b/cache_test.go index 8328588..caf8b3b 100644 --- a/cache_test.go +++ b/cache_test.go @@ -4,7 +4,7 @@ import ( "runtime" "testing" - "github.com/mgnsk/evcache/v3" + "github.com/mgnsk/evcache/v4" ) func TestCacheGoGC(t *testing.T) { diff --git a/doc.go b/doc.go index a6fc5a3..c4c0a57 100644 --- a/doc.go +++ b/doc.go @@ -1,4 +1,4 @@ /* -Package evcache implements a key-value cache with capacity overflow eviction, item expiry and deduplication. +Package evcache implements a concurrent key-value cache with capacity overflow eviction, item expiry and deduplication. */ package evcache diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..4f207aa --- /dev/null +++ b/example/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "time" + + "github.com/mgnsk/evcache/v4" +) + +func main() { + c := evcache.New[string, string]( + evcache.WithCapacity(128), + evcache.WithPolicy(evcache.LRU), + evcache.WithTTL(time.Minute), + ) + + // Fetches an existing value or calls the callback to get a new value. + result, err := c.Fetch("key", func() (string, error) { + // Possibly a very long network call. It only blocks write access to this key. + // Read access for this key returns as if the value does not exist. + return "value", nil + }) + + if err != nil { + panic(err) + } + + // Use the result. + println(result) +} diff --git a/go.mod b/go.mod index 379ef2e..0413188 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/mgnsk/evcache/v3 +module github.com/mgnsk/evcache/v4 go 1.22 diff --git a/internal/backend/backend_test.go b/internal/backend/backend_test.go index 456f46b..348599f 100644 --- a/internal/backend/backend_test.go +++ b/internal/backend/backend_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" - "github.com/mgnsk/evcache/v3/internal/backend" - . "github.com/mgnsk/evcache/v3/internal/testing" + "github.com/mgnsk/evcache/v4/internal/backend" + . "github.com/mgnsk/evcache/v4/internal/testing" ) func TestFetchCallbackBlocks(t *testing.T) { diff --git a/internal/backend/benchmark_test.go b/internal/backend/benchmark_test.go index 5ba12b7..9b18977 100644 --- a/internal/backend/benchmark_test.go +++ b/internal/backend/benchmark_test.go @@ -9,7 +9,7 @@ import ( "sync/atomic" "testing" - "github.com/mgnsk/evcache/v3/internal/backend" + "github.com/mgnsk/evcache/v4/internal/backend" ) func BenchmarkSliceLoop(b *testing.B) { diff --git a/internal/backend/element_test.go b/internal/backend/element_test.go index a950660..02d442a 100644 --- a/internal/backend/element_test.go +++ b/internal/backend/element_test.go @@ -4,8 +4,8 @@ import ( "reflect" "testing" - "github.com/mgnsk/evcache/v3/internal/backend" - . "github.com/mgnsk/evcache/v3/internal/testing" + "github.com/mgnsk/evcache/v4/internal/backend" + . "github.com/mgnsk/evcache/v4/internal/testing" ) func TestRecordSize(t *testing.T) { diff --git a/options.go b/options.go index 3ccf35a..4a0c18e 100644 --- a/options.go +++ b/options.go @@ -3,7 +3,7 @@ package evcache import ( "time" - "github.com/mgnsk/evcache/v3/internal/backend" + "github.com/mgnsk/evcache/v4/internal/backend" ) // Available cache eviction policies.