Skip to content

Commit

Permalink
Basic example, cleanup goroutine example
Browse files Browse the repository at this point in the history
  • Loading branch information
parMaster committed Jul 13, 2023
1 parent 062e48b commit 2a222ca
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ Import the `mcache` package in your Go code:
import "github.com/parMaster/mcache"
```

Create a new cache instance using the `NewCache` constructor:
Create a new cache instance using the `NewCache` constructor, and use it to perform cache operations:

```go
cache := mcache.NewCache()
data, err := cache.Get("key")
if err != nil {
data = ExpensiveFunctionCall()
cache.Set(key, data, 5*60) // cache data for 5 minutes
}
```

### Set
Expand Down Expand Up @@ -124,6 +129,17 @@ Cleanup expired key-value pairs in the cache. You can call this method periodica
cache.Cleanup()
```

If you want to cleanup expired key-value periodically, you can run the `Cleanup` method in a goroutine with a time interval:

```go
go func() {
for {
cache.Cleanup()
time.Sleep(1 * time.Minute) // run every minute
}
}()
```

## Examples

See the [examples](https://github.com/parMaster/mcache/tree/main/examples) directory for more examples.
Expand Down

0 comments on commit 2a222ca

Please sign in to comment.