mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
document WithCache function
This commit is contained in:
parent
2bbee6671a
commit
752fdf5c80
|
@ -10,12 +10,19 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WithCache(key string, obj interface{}, do func() (interface{}, error)) error {
|
|
||||||
|
type DataFetcher func() (interface{}, error)
|
||||||
|
|
||||||
|
// WithCache let you use the cache with the given cache key, variable reference and your data fetcher,
|
||||||
|
// The key must be an unique ID.
|
||||||
|
// obj is the pointer of your local variable
|
||||||
|
// fetcher is the closure that will fetch your remote data or some slow operation.
|
||||||
|
func WithCache(key string, obj interface{}, fetcher DataFetcher) error {
|
||||||
cacheDir := CacheDir()
|
cacheDir := CacheDir()
|
||||||
cacheFile := path.Join(cacheDir, key+ ".json")
|
cacheFile := path.Join(cacheDir, key+ ".json")
|
||||||
|
|
||||||
if _, err := os.Stat(cacheFile); os.IsNotExist(err) {
|
if _, err := os.Stat(cacheFile); os.IsNotExist(err) {
|
||||||
data, err := do()
|
data, err := fetcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user