diff --git a/cointop/cache.go b/cointop/cache.go index 74925e1..755eed5 100644 --- a/cointop/cache.go +++ b/cointop/cache.go @@ -10,10 +10,24 @@ import ( // CacheKey returns cached value given key func (ct *Cointop) CacheKey(key string) string { - log.Debug("CacheKey()") return strings.ToLower(fmt.Sprintf("%s_%s", ct.apiChoice, key)) } +// CompositeCacheKey returns a CacheKey for a coin (or globaldata) +func (ct *Cointop) CompositeCacheKey(symbol string, name string, convert string, chartRange string) string { + keyname := symbol + if name != "" { + keyname += "-" + name + } + if convert != "" { + keyname += "_" + convert + } + if chartRange != "" { + keyname += "_" + strings.Replace(chartRange, " ", "", -1) // "All Time" contains space + } + return ct.CacheKey(keyname) +} + // CacheAllCoinsSlugMap writes the coins map to the memory and disk cache func (ct *Cointop) CacheAllCoinsSlugMap() { log.Debug("CacheAllCoinsSlugMap()") diff --git a/cointop/chart.go b/cointop/chart.go index f0094c7..92b5120 100644 --- a/cointop/chart.go +++ b/cointop/chart.go @@ -3,7 +3,6 @@ package cointop import ( "fmt" "sort" - "strings" "sync" "time" @@ -127,7 +126,7 @@ func (ct *Cointop) ChartPoints(symbol string, name string) error { if keyname == "" { keyname = "globaldata" } - cachekey := ct.CacheKey(fmt.Sprintf("%s_%s", keyname, strings.Replace(ct.State.selectedChartRange, " ", "", -1))) + cachekey := ct.CompositeCacheKey(keyname, name, ct.State.currencyConversion, ct.State.selectedChartRange) cached, found := ct.cache.Get(cachekey) if found { @@ -219,7 +218,7 @@ func (ct *Cointop) PortfolioChart() error { } var graphData []float64 - cachekey := strings.ToLower(fmt.Sprintf("%s_%s_%s", p.Symbol, convert, strings.Replace(selectedChartRange, " ", "", -1))) + cachekey := ct.CompositeCacheKey(p.Symbol, p.Name, convert, selectedChartRange) cached, found := ct.cache.Get(cachekey) if found { // cache hit diff --git a/cointop/cointop.go b/cointop/cointop.go index dbbaa89..ebbd4b0 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -445,7 +445,7 @@ func NewCointop(config *Config) (*Cointop, error) { }) var globaldata []float64 - chartcachekey := ct.CacheKey(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.State.selectedChartRange, " ", "", -1))) + chartcachekey := ct.CompositeCacheKey("globaldata", "", "", ct.State.selectedChartRange) if ct.filecache != nil { ct.filecache.Get(chartcachekey, &globaldata) } diff --git a/cointop/layout.go b/cointop/layout.go index f056130..3800b24 100644 --- a/cointop/layout.go +++ b/cointop/layout.go @@ -1,9 +1,6 @@ package cointop import ( - "fmt" - "strings" - log "github.com/sirupsen/logrus" ) @@ -99,7 +96,7 @@ func (ct *Cointop) layout() error { ct.Views.Chart.SetBgColor(ct.colorscheme.GocuiBgColor(ct.Views.Chart.Name())) go func() { ct.UpdateChart() - cachekey := strings.ToLower(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.State.selectedChartRange, " ", "", -1))) + cachekey := ct.CompositeCacheKey("globaldata", "", "", ct.State.selectedChartRange) _, found := ct.cache.Get(cachekey) if found { ct.cache.Delete(cachekey) diff --git a/cointop/marketbar.go b/cointop/marketbar.go index 3adf857..6e040e7 100644 --- a/cointop/marketbar.go +++ b/cointop/marketbar.go @@ -100,7 +100,7 @@ func (ct *Cointop) UpdateMarketbar() error { var market types.GlobalMarketData var err error - cachekey := ct.CacheKey("market") + cachekey := ct.CompositeCacheKey("market", "", ct.State.currencyConversion, "") cached, found := ct.cache.Get(cachekey) if found {