Skip to content

Commit

Permalink
Add no-cache option
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Aug 4, 2020
1 parent 71a26ba commit d9ea155
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 35 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand Down
9 changes: 9 additions & 0 deletions cmd/cointop/cointop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/miguelmota/cointop/cointop/cmd"
)

func main() {
cmd.Execute()
}
4 changes: 3 additions & 1 deletion cointop/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
20 changes: 13 additions & 7 deletions cointop/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/cointop.go → cointop/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
64 changes: 48 additions & 16 deletions cointop/cointop.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type Config struct {
HideMarketbar bool
HideChart bool
HideStatusbar bool
NoCache bool
OnlyTable bool
RefreshRate *uint
}
Expand Down Expand Up @@ -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),
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -417,6 +429,8 @@ func Clean(config *CleanConfig) error {
config = &CleanConfig{}
}

cacheCleaned := false

cacheDir := filecache.DefaultCacheDir
if config.CacheDir != "" {
cacheDir = config.CacheDir
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions cointop/marketbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/miguelmota/cointop/cmd"
"github.com/miguelmota/cointop/cointop/cmd"
)

func main() {
Expand Down

0 comments on commit d9ea155

Please sign in to comment.