From 86e5b15991d493399bd80524d89fc246ab6109ab Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Fri, 20 Apr 2018 19:55:05 -0700 Subject: [PATCH] hard cache write Former-commit-id: 272ff932e8fc32a780c42bd8cfa6822b2c224ca3 [formerly 272ff932e8fc32a780c42bd8cfa6822b2c224ca3 [formerly e69dcc748baf0c2eb5c8711d8468d088e0c14eaa [formerly 42eb0785867baa508d01a8e3f3995424093b7849]]] Former-commit-id: 90a2bcf5dd59b21284e8c366d39699e0f2328567 Former-commit-id: ab3ac83ce1225e1563d6deb07054ed30211410f1 [formerly 9ec35d3ac676970db27213ef942c866c962e1026] Former-commit-id: f19a1a2092a9c62c9095ece79de81e172f6ea4e0 --- Makefile | 5 ++++- cointop/cache.go | 33 +++++++++++++++++++++++++++------ cointop/chart.go | 10 ++++++++++ cointop/cointop.go | 22 ++++++++++++++++++++++ cointop/config.go | 4 +++- cointop/list.go | 20 +++----------------- cointop/marketbar.go | 3 +++ 7 files changed, 72 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 2a78454..a537b05 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ all: run: go run main.go +debug: + DEBUG=1 go run main.go + # http://macappstore.org/upx build: clean env GOARCH=amd64 go build -ldflags "-s -w" -o bin/cointop64 && upx bin/cointop64 && \ @@ -23,7 +26,7 @@ snap/clean: snap/stage: snapcraft stage -snap/build: snap/clean snap/stage +snap/build: snap/clean snap/stage snapcraft snap snap/deploy: diff --git a/cointop/cache.go b/cointop/cache.go index 16a000b..52bd44c 100644 --- a/cointop/cache.go +++ b/cointop/cache.go @@ -4,19 +4,29 @@ import ( "encoding/json" "fmt" "io/ioutil" + "os" ) -func (ct *Cointop) writeCache(data []byte) error { - path := ct.cachePath() - err := ioutil.WriteFile(path, data, 0644) +func (ct *Cointop) writeHardCache(data interface{}, filename string) error { + b, err := json.Marshal(data) + if err != nil { + return err + } + path := fmt.Sprintf("%s/.%s", ct.hardCachePath(), filename) + of, err := os.Create(path) + defer of.Close() + if err != nil { + return err + } + _, err = of.Write(b) if err != nil { return err } return nil } -func (ct *Cointop) readCache(i interface{}) (interface{}, bool, error) { - path := ct.cachePath() +func (ct *Cointop) readHardCache(i interface{}, filename string) (interface{}, bool, error) { + path := fmt.Sprintf("%s/.%s", ct.hardCachePath(), filename) b, err := ioutil.ReadFile(path) if err != nil { return nil, false, err @@ -28,6 +38,17 @@ func (ct *Cointop) readCache(i interface{}) (interface{}, bool, error) { return i, true, nil } -func (ct *Cointop) cachePath() string { +func (ct *Cointop) hardCachePath() string { return fmt.Sprintf("%v%v", ct.configDirPath(), "/.cache") } + +func (ct *Cointop) createCacheDir() error { + path := ct.hardCachePath() + if _, err := os.Stat(path); os.IsNotExist(err) { + err := os.MkdirAll(path, os.ModePerm) + if err != nil { + return err + } + } + return nil +} diff --git a/cointop/chart.go b/cointop/chart.go index ca34b5c..a1aba3e 100644 --- a/cointop/chart.go +++ b/cointop/chart.go @@ -2,6 +2,7 @@ package cointop import ( "fmt" + "strings" "time" "github.com/miguelmota/cointop/pkg/color" @@ -45,6 +46,11 @@ func (ct *Cointop) chartPoints(maxX int, coin string) error { end := secs var data []float64 + filename := strings.ToLower(coin) + if filename == "" { + filename = "globaldata" + } + if coin == "" { graphData, err := ct.api.GetGlobalMarketGraphData(start, end) if err != nil { @@ -63,6 +69,10 @@ func (ct *Cointop) chartPoints(maxX int, coin string) error { } } + go func() { + _ = ct.writeHardCache(data, filename) + }() + chart.Data = data termui.Body = termui.NewGrid() termui.Body.Width = maxX diff --git a/cointop/cointop.go b/cointop/cointop.go index d42a964..18a68a1 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -72,6 +72,10 @@ func Run() { if err != nil { log.Fatal(err) } + err = ct.createCacheDir() + if err != nil { + log.Fatal(err) + } g, err := gocui.NewGui(gocui.Output256) if err != nil { log.Fatalf("new gocui: %v", err) @@ -87,6 +91,24 @@ func Run() { if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Fatalf("main loop: %v", err) } + /* + ifc, ok, _ := ct.readHardCache(&data, filename) + if ok { + // hard cache hit + if ifc != nil { + ct.debuglog("hard cache hit") + } + } + */ + /* + ifc, ok, _ := ct.readHardCache(&allcoinsmap, "allcoinsmap") + if ok { + // hard cache hit + if ifc != nil { + ct.debuglog("hard cache hit") + } + } + */ } func (ct *Cointop) quit() error { diff --git a/cointop/config.go b/cointop/config.go index 7100d81..0d20b1b 100644 --- a/cointop/config.go +++ b/cointop/config.go @@ -10,6 +10,8 @@ import ( "github.com/BurntSushi/toml" ) +var fileperm = os.FileMode(0644) + type config struct { Shortcuts map[string]interface{} `toml:"shortcuts"` Favorites map[string][]interface{} `toml:"favorites"` @@ -98,7 +100,7 @@ func (ct *Cointop) saveConfig() error { if err != nil { return err } - err = ioutil.WriteFile(path, b, 0644) + err = ioutil.WriteFile(path, b, fileperm) if err != nil { return err } diff --git a/cointop/list.go b/cointop/list.go index af56d3a..15199a1 100644 --- a/cointop/list.go +++ b/cointop/list.go @@ -1,7 +1,6 @@ package cointop import ( - "encoding/json" "time" types "github.com/miguelmota/cointop/pkg/api/types" @@ -18,14 +17,6 @@ func (ct *Cointop) updateCoins() error { // cache hit allcoinsmap, _ = cached.(map[string]types.Coin) ct.debuglog("soft cache hit") - } else { - ifc, ok, _ := ct.readCache(&allcoinsmap) - if ok { - // hard cache hit - if ifc != nil { - ct.debuglog("hard cache hit") - } - } } // cache miss @@ -36,14 +27,9 @@ func (ct *Cointop) updateCoins() error { return err } ct.cache.Set(cachekey, allcoinsmap, 10*time.Second) - b, err := json.Marshal(allcoinsmap) - if err != nil { - return err - } - err = ct.writeCache(b) - if err != nil { - return err - } + go func() { + _ = ct.writeHardCache(allcoinsmap, "allcoinsmap") + }() } if len(ct.allcoinsmap) == 0 { diff --git a/cointop/marketbar.go b/cointop/marketbar.go index 491d754..b205cf1 100644 --- a/cointop/marketbar.go +++ b/cointop/marketbar.go @@ -14,6 +14,9 @@ func (ct *Cointop) updateMarketbar() error { if err != nil { return err } + go func() { + _ = ct.writeHardCache(market, "market") + }() timeframe := "7 Day" chartname := ct.selectedCoinName() if chartname == "" {