Skip to content

Commit

Permalink
add config example
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning committed Jul 16, 2023
1 parent 7f051c3 commit 7ac470f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ A simple memoizer for Go.

[![Reference](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/LightningDev1/go-memoizer)
[![Linter](https://goreportcard.com/badge/github.com/LightningDev1/go-memoizer?style=flat-square)](https://goreportcard.com/report/github.com/LightningDev1/go-memoizer)
[![Build status](https://github.com/LightningDev1/go-memoizer/actions/workflows/ci.yml/badge.svg)](https://github.com/LightningDev1/go-memoizer/actions)
[![Build status](https://github.com/LightningDev1/go-memoizer/actions/workflows/ci.yml/badge.svg)](https://github.com/LightningDev1/go-memoizer/actions)

## Installation

```bash
go get github.com/LightningDev1/go-memoizer
```

## Usage

See [config example](./examples/config/main.go) for an example.
52 changes: 52 additions & 0 deletions examples/config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/LightningDev1/go-memoizer"
)

// ProgramConfig is the config for the program.
type ProgramConfig map[string]any

// GetConfig gets the config for the program.
func GetConfig() (config *ProgramConfig, err error) {
content, err := os.ReadFile("./config.json")
if err != nil {
return nil, err
}

err = json.Unmarshal(content, &config)
if err != nil {
return nil, err
}

fmt.Println("Loaded config!")

return config, nil
}

// ConfigMemoizer is the memoizer for the config.
var ConfigMemoizer = memoizer.NewMemoizer(GetConfig, time.Second*5)

func doSomething(i int) {
config, err := ConfigMemoizer.Get()
if err != nil {
panic(err)
}

// Do something with the config.

fmt.Println(i, config)
}

func main() {
for i := 0; i < 50; i++ {
doSomething(i)

time.Sleep(time.Second)
}
}

0 comments on commit 7ac470f

Please sign in to comment.