diff --git a/README.md b/README.md index d9bc59c..f111aef 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,4 @@ -# mcache - -[![codecov](https://codecov.io/gh/parMaster/mcache/branch/main/graph/badge.svg?token=K6685ZN3YS)](https://codecov.io/gh/parMaster/mcache) -![GitHub](https://img.shields.io/github/license/parMaster/mcache) -[![Go Report Card](https://goreportcard.com/badge/github.com/parMaster/mcache)](https://goreportcard.com/report/github.com/parMaster/mcache) -[![Go](https://github.com/parMaster/zoomrs/actions/workflows/go.yml/badge.svg)](https://github.com/parMaster/zoomrs/actions/workflows/go.yml) +# mcache [![codecov](https://codecov.io/gh/parMaster/mcache/branch/main/graph/badge.svg?token=K6685ZN3YS)](https://codecov.io/gh/parMaster/mcache) ![GitHub](https://img.shields.io/github/license/parMaster/mcache) [![Go Report Card](https://goreportcard.com/badge/github.com/parMaster/mcache)](https://goreportcard.com/report/github.com/parMaster/mcache) [![Go](https://github.com/parMaster/zoomrs/actions/workflows/go.yml/badge.svg)](https://github.com/parMaster/zoomrs/actions/workflows/go.yml) `mcache` is a simple, fast, thread-safe in-memory cache library with by-key TTL written in Go. @@ -43,7 +38,11 @@ if err != nil { cache.Set("key", data, 5*60) // cache data for 5 minutes } ``` +## Examples + +See the [examples](https://github.com/parMaster/mcache/tree/main/examples) directory for more examples. +## API Reference ### Set Set a key-value pair in the cache. The key must be a string, and the value can be any type that implements the `interface{}` interface, ttl is int64 value in seconds: @@ -145,10 +144,6 @@ go func() { }() ``` -## Examples - -See the [examples](https://github.com/parMaster/mcache/tree/main/examples) directory for more examples. - ## Tests and Benchmarks 100% test coverage: diff --git a/examples/readme_example/main.go b/examples/readme_example/main.go new file mode 100644 index 0000000..ee3cdc9 --- /dev/null +++ b/examples/readme_example/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + + "github.com/parMaster/mcache" +) + +func main() { + + cache := mcache.NewCache() + + cache.Set("key", "value", 5*60) // set value with expiration in 5 minutes + + v, err := cache.Get("key") + if err != nil { + // either error can be checked + fmt.Println(err) + } + if v != nil { + // or value can be checked for nil + fmt.Println("key =", v) + } + + // check if key exists + exists, err := cache.Has("key") + if err != nil { + // possible errors: + // mcache.ErrKeyNotFound - key doesn't exist + // mcache.ErrExpired - key expired + fmt.Println(err) + } + if exists { + fmt.Println("key exists") + } + + // delete key + cache.Del("key") +}