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
pull/15/head
Miguel Mota 6 years ago
parent ba562229f2
commit 86e5b15991

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

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

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

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

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

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

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

Loading…
Cancel
Save