Skip to content

Commit

Permalink
new example
Browse files Browse the repository at this point in the history
  • Loading branch information
parMaster committed Jul 21, 2023
1 parent 77094e3 commit 5235e39
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
39 changes: 39 additions & 0 deletions examples/readme_example/main.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 5235e39

Please sign in to comment.