Add no-cache option

pull/58/head 1.4.8
Miguel Mota 4 years ago
parent 71a26baae9
commit d9ea155004

@ -28,18 +28,18 @@ debug:
DEBUG=1 go run main.go DEBUG=1 go run main.go
build: build:
go build -o bin/cointop main.go go build -ldflags "-X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop main.go
# http://macappstore.org/upx # http://macappstore.org/upx
build-mac: clean-mac build-mac: clean-mac
env GOARCH=amd64 go build -ldflags "-s -w" -o bin/macos/cointop && upx bin/macos/cointop env GOARCH=amd64 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/macos/cointop && upx bin/macos/cointop
build-linux: clean-linux build-linux: clean-linux
env GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o bin/linux/cointop && upx bin/linux/cointop env GOOS=linux GOARCH=amd64 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/linux/cointop && upx bin/linux/cointop
build-multiple: clean build-multiple: clean
env GOARCH=amd64 go build -ldflags "-s -w" -o bin/cointop64 && upx bin/cointop64 && \ env GOARCH=amd64 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop64 && upx bin/cointop64 && \
env GOARCH=386 go build -ldflags "-s -w" -o bin/cointop32 && upx bin/cointop32 env GOARCH=386 go build -ldflags "-s -w -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop32 && upx bin/cointop32
clean-mac: clean-mac:
go clean && \ go clean && \

@ -0,0 +1,9 @@
package main
import (
"github.com/miguelmota/cointop/cointop/cmd"
)
func main() {
cmd.Execute()
}

@ -25,6 +25,8 @@ func (ct *Cointop) CacheAllCoinsSlugMap() {
if len(allCoinsSlugMap) != 0 { if len(allCoinsSlugMap) != 0 {
cachekey := ct.CacheKey("allCoinsSlugMap") cachekey := ct.CacheKey("allCoinsSlugMap")
ct.cache.Set(cachekey, allCoinsSlugMap, 10*time.Second) ct.cache.Set(cachekey, allCoinsSlugMap, 10*time.Second)
if ct.filecache != nil {
ct.filecache.Set(cachekey, allCoinsSlugMap, 24*time.Hour) ct.filecache.Set(cachekey, allCoinsSlugMap, 24*time.Hour)
} }
}
} }

@ -174,10 +174,12 @@ func (ct *Cointop) ChartPoints(symbol string, name string) error {
} }
ct.cache.Set(cachekey, data, 10*time.Second) ct.cache.Set(cachekey, data, 10*time.Second)
if ct.filecache != nil {
go func() { go func() {
ct.filecache.Set(cachekey, data, 24*time.Hour) ct.filecache.Set(cachekey, data, 24*time.Hour)
}() }()
} }
}
chart.Data = data chart.Data = data
termui.Body = termui.NewGrid() termui.Body = termui.NewGrid()
@ -260,7 +262,9 @@ func (ct *Cointop) PortfolioChart() error {
graphData, _ = cached.([]float64) graphData, _ = cached.([]float64)
ct.debuglog("soft cache hit") ct.debuglog("soft cache hit")
} else { } else {
if ct.filecache != nil {
ct.filecache.Get(cachekey, &graphData) ct.filecache.Get(cachekey, &graphData)
}
if len(graphData) == 0 { if len(graphData) == 0 {
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
@ -277,10 +281,12 @@ func (ct *Cointop) PortfolioChart() error {
} }
ct.cache.Set(cachekey, graphData, 10*time.Second) ct.cache.Set(cachekey, graphData, 10*time.Second)
if ct.filecache != nil {
go func() { go func() {
ct.filecache.Set(cachekey, graphData, 24*time.Hour) ct.filecache.Set(cachekey, graphData, 24*time.Hour)
}() }()
} }
}
for i := range graphData { for i := range graphData {
price := graphData[i] price := graphData[i]

@ -12,7 +12,7 @@ import (
// Execute executes the program // Execute executes the program
func Execute() { func Execute() {
var version, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable, silent bool var version, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable, silent, noCache bool
var refreshRate uint var refreshRate uint
var config, cmcAPIKey, apiChoice, colorscheme, coin, currency string var config, cmcAPIKey, apiChoice, colorscheme, coin, currency string
cacheDir := filecache.DefaultCacheDir cacheDir := filecache.DefaultCacheDir
@ -68,6 +68,7 @@ For more information, visit: https://github.com/miguelmota/cointop`,
ct, err := cointop.NewCointop(&cointop.Config{ ct, err := cointop.NewCointop(&cointop.Config{
CacheDir: cacheDir, CacheDir: cacheDir,
NoCache: noCache,
ConfigFilepath: config, ConfigFilepath: config,
CoinMarketCapAPIKey: cmcAPIKey, CoinMarketCapAPIKey: cmcAPIKey,
APIChoice: apiChoice, APIChoice: apiChoice,
@ -95,6 +96,7 @@ For more information, visit: https://github.com/miguelmota/cointop`,
rootCmd.Flags().BoolVarP(&hideStatusbar, "hide-statusbar", "", false, "Hide the bottom statusbar") rootCmd.Flags().BoolVarP(&hideStatusbar, "hide-statusbar", "", false, "Hide the bottom statusbar")
rootCmd.Flags().BoolVarP(&onlyTable, "only-table", "", false, "Show only the table. Hides the chart and top and bottom bars") rootCmd.Flags().BoolVarP(&onlyTable, "only-table", "", false, "Show only the table. Hides the chart and top and bottom bars")
rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silence log ouput") rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Silence log ouput")
rootCmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "No cache")
rootCmd.Flags().UintVarP(&refreshRate, "refresh-rate", "r", 60, "Refresh rate in seconds. Set to 0 to not auto-refresh") rootCmd.Flags().UintVarP(&refreshRate, "refresh-rate", "r", 60, "Refresh rate in seconds. Set to 0 to not auto-refresh")
rootCmd.Flags().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath)) rootCmd.Flags().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key") rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key")

@ -131,6 +131,7 @@ type Config struct {
HideMarketbar bool HideMarketbar bool
HideChart bool HideChart bool
HideStatusbar bool HideStatusbar bool
NoCache bool
OnlyTable bool OnlyTable bool
RefreshRate *uint RefreshRate *uint
} }
@ -162,6 +163,13 @@ func NewCointop(config *Config) (*Cointop, error) {
configFilepath = config.ConfigFilepath configFilepath = config.ConfigFilepath
} }
var fcache *filecache.FileCache
if !config.NoCache {
fcache = filecache.NewFileCache(&filecache.Config{
CacheDir: config.CacheDir,
})
}
ct := &Cointop{ ct := &Cointop{
apiChoice: CoinGecko, apiChoice: CoinGecko,
apiKeys: new(APIKeys), apiKeys: new(APIKeys),
@ -174,9 +182,7 @@ func NewCointop(config *Config) (*Cointop, error) {
debug: debug, debug: debug,
chartRangesMap: ChartRangesMap(), chartRangesMap: ChartRangesMap(),
limiter: time.Tick(2 * time.Second), limiter: time.Tick(2 * time.Second),
filecache: filecache.NewFileCache(&filecache.Config{ filecache: fcache,
CacheDir: config.CacheDir,
}),
State: &State{ State: &State{
allCoins: []*Coin{}, allCoins: []*Coin{},
currencyConversion: "USD", currencyConversion: "USD",
@ -293,7 +299,9 @@ func NewCointop(config *Config) (*Cointop, error) {
allCoinsSlugMap := make(map[string]*Coin) allCoinsSlugMap := make(map[string]*Coin)
coinscachekey := ct.CacheKey("allCoinsSlugMap") coinscachekey := ct.CacheKey("allCoinsSlugMap")
if ct.filecache != nil {
ct.filecache.Get(coinscachekey, &allCoinsSlugMap) ct.filecache.Get(coinscachekey, &allCoinsSlugMap)
}
for k, v := range allCoinsSlugMap { for k, v := range allCoinsSlugMap {
ct.State.allCoinsSlugMap.Store(k, v) ct.State.allCoinsSlugMap.Store(k, v)
@ -332,12 +340,16 @@ func NewCointop(config *Config) (*Cointop, error) {
var globaldata []float64 var globaldata []float64
chartcachekey := ct.CacheKey(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.State.selectedChartRange, " ", "", -1))) chartcachekey := ct.CacheKey(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.State.selectedChartRange, " ", "", -1)))
if ct.filecache != nil {
ct.filecache.Get(chartcachekey, &globaldata) ct.filecache.Get(chartcachekey, &globaldata)
}
ct.cache.Set(chartcachekey, globaldata, 10*time.Second) ct.cache.Set(chartcachekey, globaldata, 10*time.Second)
var market types.GlobalMarketData var market types.GlobalMarketData
marketcachekey := ct.CacheKey("market") marketcachekey := ct.CacheKey("market")
if ct.filecache != nil {
ct.filecache.Get(marketcachekey, &market) ct.filecache.Get(marketcachekey, &market)
}
ct.cache.Set(marketcachekey, market, 10*time.Second) ct.cache.Set(marketcachekey, market, 10*time.Second)
// TODO: notify offline status in status bar // TODO: notify offline status in status bar
@ -417,6 +429,8 @@ func Clean(config *CleanConfig) error {
config = &CleanConfig{} config = &CleanConfig{}
} }
cacheCleaned := false
cacheDir := filecache.DefaultCacheDir cacheDir := filecache.DefaultCacheDir
if config.CacheDir != "" { if config.CacheDir != "" {
cacheDir = config.CacheDir cacheDir = config.CacheDir
@ -437,13 +451,17 @@ func Clean(config *CleanConfig) error {
if err := os.Remove(file); err != nil { if err := os.Remove(file); err != nil {
return err return err
} }
cacheCleaned = true
} }
} }
} }
if config.Log { if config.Log {
if cacheCleaned {
fmt.Println("cointop cache has been cleaned") fmt.Println("cointop cache has been cleaned")
} }
}
return nil return nil
} }
@ -467,20 +485,34 @@ func Reset(config *ResetConfig) error {
return err return err
} }
// default config path configDeleted := false
configPath := fmt.Sprintf("%s%s", pathutil.UserPreferredHomeDir(), "/.cointop")
if _, err := os.Stat(configPath); !os.IsNotExist(err) { possibleConfigPaths := []string{
"~/.config/cointop/config.toml",
"~/.config/cointop/config",
"~/.cointop/config",
"~/.cointop/config.toml",
}
for _, configPath := range possibleConfigPaths {
normalizedPath := pathutil.NormalizePath(configPath)
if _, err := os.Stat(normalizedPath); !os.IsNotExist(err) {
if config.Log { if config.Log {
fmt.Printf("removing %s\n", configPath) fmt.Printf("removing %s\n", normalizedPath)
} }
if err := os.RemoveAll(configPath); err != nil { if err := os.RemoveAll(normalizedPath); err != nil {
return err return err
} }
configDeleted = true
}
} }
if config.Log { if config.Log {
if configDeleted {
fmt.Println("cointop has been reset") fmt.Println("cointop has been reset")
} }
}
return nil return nil
} }

@ -106,14 +106,18 @@ func (ct *Cointop) UpdateMarketbar() error {
if market.TotalMarketCapUSD == 0 { if market.TotalMarketCapUSD == 0 {
market, err = ct.api.GetGlobalMarketData(ct.State.currencyConversion) market, err = ct.api.GetGlobalMarketData(ct.State.currencyConversion)
if err != nil { if err != nil {
if ct.filecache != nil {
ct.filecache.Get(cachekey, &market) ct.filecache.Get(cachekey, &market)
} }
}
ct.cache.Set(cachekey, market, 10*time.Second) ct.cache.Set(cachekey, market, 10*time.Second)
if ct.filecache != nil {
go func() { go func() {
ct.filecache.Set(cachekey, market, 24*time.Hour) ct.filecache.Set(cachekey, market, 24*time.Hour)
}() }()
} }
}
timeframe := ct.State.selectedChartRange timeframe := ct.State.selectedChartRange
chartname := ct.SelectedCoinName() chartname := ct.SelectedCoinName()

@ -1,7 +1,7 @@
package main package main
import ( import (
"github.com/miguelmota/cointop/cmd" "github.com/miguelmota/cointop/cointop/cmd"
) )
func main() { func main() {

Loading…
Cancel
Save