Skip to content

Commit

Permalink
Update module and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsk committed Aug 9, 2024
1 parent e4d66b3 commit a8396e5
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 33 deletions.
50 changes: 31 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
2 changes: 1 addition & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync/atomic"
"testing"

"github.com/mgnsk/evcache/v3"
"github.com/mgnsk/evcache/v4"
)

func BenchmarkFetchAndEvictParallel(b *testing.B) {
Expand Down
5 changes: 1 addition & 4 deletions cache.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"runtime"
"testing"

"github.com/mgnsk/evcache/v3"
"github.com/mgnsk/evcache/v4"
)

func TestCacheGoGC(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/mgnsk/evcache/v3
module github.com/mgnsk/evcache/v4

go 1.22

Expand Down
4 changes: 2 additions & 2 deletions internal/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/backend/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions internal/backend/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a8396e5

Please sign in to comment.