diff --git a/README.md b/README.md index 9460f5b..46d44ea 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # mcache -`mcache` is a simple and thread-safe in-memory cache library for Go. +`mcache` is a simple, fast, thread-safe in-memory cache library with by-key TTL written in Go. ## Features @@ -144,9 +144,19 @@ go func() { See the [examples](https://github.com/parMaster/mcache/tree/main/examples) directory for more examples. -## Benchmarks +## Tests and Benchmarks + +100% test coverage: + +```shell +$ go test -race . + +ok github.com/parMaster/mcache 8.239s coverage: 100.0% of statements +``` +Blinding fast and efficient: + ```shell -Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -coverprofile=/tmp/vscode-gopk5hiw/go-code-cover -bench . github.com/parMaster/mcache +$ go test -bench . -benchmem goos: linux goarch: amd64 diff --git a/main_test.go b/main_test.go index eb824e2..26ddf83 100644 --- a/main_test.go +++ b/main_test.go @@ -74,7 +74,36 @@ func Test_SimpleTest_Mcache(t *testing.T) { assert.Equal(t, ErrKeyNotFound, err.Error()) } + c.Set("key", "value", 1) + time.Sleep(time.Second * 2) + err = c.Set("key", "newvalue", 1) + assert.NoError(t, err) + + // old value should be rewritten + value, err := c.Get("key") + assert.NoError(t, err) + assert.Equal(t, "newvalue", value) + + err = c.Set("key", "not a newer value", 1) + assert.Equal(t, ErrKeyExists, err.Error()) + + time.Sleep(time.Second * 2) + err = c.Set("key", "even newer value", 1) + // key should be silently rewritten + assert.NoError(t, err) + value, err = c.Get("key") + assert.NoError(t, err) + assert.Equal(t, "even newer value", value) + + time.Sleep(time.Second * 2) c.Cleanup() + // key should be deleted + has, err = c.Has("key") + assert.False(t, has) + + // del should return error if key doesn't exist + err = c.Del("noSuchKey") + assert.Error(t, err) err = c.Clear() assert.NoError(t, err)