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
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
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
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
env GOARCH=amd64 go build -ldflags "-s -w" -o bin/cointop64 && upx bin/cointop64 && \
env GOARCH=386 go build -ldflags "-s -w" -o bin/cointop32 && upx bin/cointop32
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 -X github.com/miguelmota/cointop/cointop.version=$(VERSION)" -o bin/cointop32 && upx bin/cointop32
clean-mac:
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 {
cachekey := ct.CacheKey("allCoinsSlugMap")
ct.cache.Set(cachekey, allCoinsSlugMap, 10*time.Second)
ct.filecache.Set(cachekey, allCoinsSlugMap, 24*time.Hour)
if ct.filecache != nil {
ct.filecache.Set(cachekey, allCoinsSlugMap, 24*time.Hour)
}
}
}

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

@ -12,7 +12,7 @@ import (
// Execute executes the program
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 config, cmcAPIKey, apiChoice, colorscheme, coin, currency string
cacheDir := filecache.DefaultCacheDir
@ -68,6 +68,7 @@ For more information, visit: https://github.com/miguelmota/cointop`,
ct, err := cointop.NewCointop(&cointop.Config{
CacheDir: cacheDir,
NoCache: noCache,
ConfigFilepath: config,
CoinMarketCapAPIKey: cmcAPIKey,
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(&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(&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().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key")

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

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

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

Loading…
Cancel
Save