-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lightning
committed
Jul 16, 2023
1 parent
7f051c3
commit 7ac470f
Showing
2 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |