Use common function to generate cache keys, and fix GlobalMarketData vs Conversion caching bug

pull/177/head
Simon Roberts 3 years ago
parent aba283443d
commit 9c86ae5de7
No known key found for this signature in database
GPG Key ID: 0F30F99E6B771FD4

@ -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()")

@ -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

@ -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)
}

@ -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)

@ -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 {

Loading…
Cancel
Save