From 752fdf5c80ae0ce01ef10b7576c66c6063249693 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 20 Oct 2020 12:22:18 +0800 Subject: [PATCH] document WithCache function --- pkg/bbgo/cache.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/bbgo/cache.go b/pkg/bbgo/cache.go index 49e84226d..cc6267c2a 100644 --- a/pkg/bbgo/cache.go +++ b/pkg/bbgo/cache.go @@ -10,12 +10,19 @@ import ( "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() cacheFile := path.Join(cacheDir, key+ ".json") if _, err := os.Stat(cacheFile); os.IsNotExist(err) { - data, err := do() + data, err := fetcher() if err != nil { return err }