Skip to content
This repository has been archived by the owner on Jul 22, 2019. It is now read-only.

Commit

Permalink
adds a cache file (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates authored Jan 14, 2019
1 parent 0c1b5e0 commit 7a4c4d3
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 9 deletions.
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@ module github.com/gobuffalo/buffalo-plugins

require (
github.com/BurntSushi/toml v0.3.1
github.com/fatih/structs v1.1.0 // indirect
github.com/gobuffalo/envy v1.6.11
github.com/gobuffalo/events v1.1.8
github.com/gobuffalo/genny v0.0.0-20190104222617-a71664fc38e7
github.com/gobuffalo/github_flavored_markdown v1.0.7 // indirect
github.com/gobuffalo/events v1.1.9
github.com/gobuffalo/genny v0.0.0-20190112155932-f31a84fcacf5
github.com/gobuffalo/licenser v0.0.0-20181211173111-f8a311c51159
github.com/gobuffalo/logger v0.0.0-20181127160119-5b956e21995c
github.com/gobuffalo/meta v0.0.0-20181127070345-0d7e59dd540b
github.com/gobuffalo/packr/v2 v2.0.0-rc.13
github.com/gobuffalo/plush v3.7.32+incompatible
github.com/gobuffalo/plushgen v0.0.0-20190104222512-177cd2b872b3
github.com/gobuffalo/release v1.1.6
github.com/gobuffalo/uuid v2.0.5+incompatible // indirect
github.com/gobuffalo/validate v2.0.3+incompatible // indirect
github.com/gofrs/uuid v3.1.0+incompatible // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/karrick/godirwalk v1.7.8
github.com/markbates/going v1.0.2
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.3.0
github.com/spf13/afero v1.2.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.1
github.com/stretchr/testify v1.3.0
Expand Down
8 changes: 7 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ github.com/gobuffalo/buffalo-plugins v1.6.11/go.mod h1:eAA6xJIL8OuynJZ8amXjRmHND
github.com/gobuffalo/buffalo-plugins v1.8.2/go.mod h1:9te6/VjEQ7pKp7lXlDIMqzxgGpjlKoAcAANdCgoR960=
github.com/gobuffalo/buffalo-plugins v1.8.3/go.mod h1:IAWq6vjZJVXebIq2qGTLOdlXzmpyTZ5iJG5b59fza5U=
github.com/gobuffalo/buffalo-plugins v1.9.4/go.mod h1:grCV6DGsQlVzQwk6XdgcL3ZPgLm9BVxlBmXPMF8oBHI=
github.com/gobuffalo/buffalo-plugins v1.10.0/go.mod h1:4osg8d9s60txLuGwXnqH+RCjPHj9K466cDFRl3PErHI=
github.com/gobuffalo/buffalo-pop v1.0.5/go.mod h1:Fw/LfFDnSmB/vvQXPvcXEjzP98Tc+AudyNWUBWKCwQ8=
github.com/gobuffalo/envy v1.6.4/go.mod h1:Abh+Jfw475/NWtYMEt+hnJWRiC8INKWibIMyNt1w2Mc=
github.com/gobuffalo/envy v1.6.5/go.mod h1:N+GkhhZ/93bGZc6ZKhJLP6+m+tCNPKwgSpH9kaifseQ=
Expand All @@ -49,6 +50,8 @@ github.com/gobuffalo/events v1.1.4/go.mod h1:09/YRRgZHEOts5Isov+g9X2xajxdvOAcUuA
github.com/gobuffalo/events v1.1.5/go.mod h1:3YUSzgHfYctSjEjLCWbkXP6djH2M+MLaVRzb4ymbAK0=
github.com/gobuffalo/events v1.1.7/go.mod h1:6fGqxH2ing5XMb3EYRq9LEkVlyPGs4oO/eLzh+S8CxY=
github.com/gobuffalo/events v1.1.8/go.mod h1:UFy+W6X6VbCWS8k2iT81HYX65dMtiuVycMy04cplt/8=
github.com/gobuffalo/events v1.1.9 h1:ukq5ys/h0TuiX7eLJyZBD1dJOy0r19JTEYmgXKG9j+Y=
github.com/gobuffalo/events v1.1.9/go.mod h1:/0nf8lMtP5TkgNbzYxR6Bl4GzBy5s5TebgNTdRfRbPM=
github.com/gobuffalo/fizz v1.0.12/go.mod h1:C0sltPxpYK8Ftvf64kbsQa2yiCZY4RZviurNxXdAKwc=
github.com/gobuffalo/flect v0.0.0-20180907193754-dc14d8acaf9f/go.mod h1:rCiQgmAE4axgBNl3jZWzS5rETRYTGOsrixTRaCPzNdA=
github.com/gobuffalo/flect v0.0.0-20181002182613-4571df4b1daf/go.mod h1:rCiQgmAE4axgBNl3jZWzS5rETRYTGOsrixTRaCPzNdA=
Expand Down Expand Up @@ -82,8 +85,9 @@ github.com/gobuffalo/genny v0.0.0-20181203201232-849d2c9534ea/go.mod h1:wpNSANu9
github.com/gobuffalo/genny v0.0.0-20181206121324-d6fb8a0dbe36/go.mod h1:wpNSANu9UErftfiaAlz1pDZclrYzLtO5lALifODyjuM=
github.com/gobuffalo/genny v0.0.0-20181207164119-84844398a37d/go.mod h1:y0ysCHGGQf2T3vOhCrGHheYN54Y/REj0ayd0Suf4C/8=
github.com/gobuffalo/genny v0.0.0-20181211165820-e26c8466f14d/go.mod h1:sHnK+ZSU4e2feXP3PA29ouij6PUEiN+RCwECjCTB3yM=
github.com/gobuffalo/genny v0.0.0-20190104222617-a71664fc38e7 h1:RPiJ0Orw/4i4JWGuvcZFYiW16+Ql1C9TyrYRznbkKjg=
github.com/gobuffalo/genny v0.0.0-20190104222617-a71664fc38e7/go.mod h1:QPsQ1FnhEsiU8f+O0qKWXz2RE4TiDqLVChWkBuh1WaY=
github.com/gobuffalo/genny v0.0.0-20190112155932-f31a84fcacf5 h1:boQS3dA9PxhyufJEWIILrG6pJQbDnpwP2rFyvWacdoY=
github.com/gobuffalo/genny v0.0.0-20190112155932-f31a84fcacf5/go.mod h1:CIaHCrSIuJ4il6ka3Hub4DR4adDrGoXGEEt2FbBxoIo=
github.com/gobuffalo/github_flavored_markdown v1.0.4/go.mod h1:uRowCdK+q8d/RF0Kt3/DSalaIXbb0De/dmTqMQdkQ4I=
github.com/gobuffalo/github_flavored_markdown v1.0.5/go.mod h1:U0643QShPF+OF2tJvYNiYDLDGDuQmJZXsf/bHOJPsMY=
github.com/gobuffalo/github_flavored_markdown v1.0.7/go.mod h1:w93Pd9Lz6LvyQXEG6DktTPHkOtCbr+arAD5mkwMzXLI=
Expand Down Expand Up @@ -178,6 +182,8 @@ github.com/gobuffalo/validate v2.0.3+incompatible/go.mod h1:N+EtDe0J8252BgfzQUCh
github.com/gobuffalo/x v0.0.0-20181003152136-452098b06085/go.mod h1:WevpGD+5YOreDJznWevcn8NTmQEW5STSBgIkpkjzqXc=
github.com/gobuffalo/x v0.0.0-20181007152206-913e47c59ca7/go.mod h1:9rDPXaB3kXdKWzMc4odGQQdG2e2DIEmANy5aSJ9yesY=
github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
Expand Down
2 changes: 1 addition & 1 deletion packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions plugins/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package plugins

import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"sync"

"github.com/gobuffalo/envy"
)

type cachedPlugin struct {
Commands Commands `json:"commands"`
CheckSum string `json:"check_sum"`
}

type cachedPlugins map[string]cachedPlugin

var cachePath = func() string {
home := "."
if usr, err := user.Current(); err == nil {
home = usr.HomeDir
}
return filepath.Join(home, ".buffalo", "plugin.cache")
}()

var cacheMoot sync.RWMutex

var cacheOn = envy.Get("BUFFALO_PLUGIN_CACHE", "on")

var cache = func() cachedPlugins {
m := cachedPlugins{}
if cacheOn != "on" {
return m
}
f, err := os.Open(cachePath)
if err != nil {
return m
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&m); err != nil {
f.Close()
os.Remove(f.Name())
}
return m
}()

func findInCache(path string) (cachedPlugin, bool) {
cacheMoot.RLock()
defer cacheMoot.RUnlock()
cp, ok := cache[path]
return cp, ok
}

func saveCache() error {
if cacheOn != "on" {
return nil
}
cacheMoot.Lock()
defer cacheMoot.Unlock()
os.MkdirAll(filepath.Dir(cachePath), 0744)
f, err := os.Create(cachePath)
if err != nil {
return err
}
return json.NewEncoder(f).Encode(cache)
}

func sum(path string) string {
f, err := os.Open(path)
if err != nil {
return ""
}
defer f.Close()
hash := sha256.New()
if _, err := io.Copy(hash, f); err != nil {
return ""
}
sum := hash.Sum(nil)

s := fmt.Sprintf("%x", sum)
return s
}

func addToCache(path string, cp cachedPlugin) {
if cp.CheckSum == "" {
cp.CheckSum = sum(path)
}
cacheMoot.Lock()
defer cacheMoot.Unlock()
cache[path] = cp
}
23 changes: 23 additions & 0 deletions plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ var _list List
func Available() (List, error) {
var err error
oncer.Do("plugins.Available", func() {
defer func() {
if err := saveCache(); err != nil {
logrus.Error(err)
}
}()

app := meta.New(".")

Expand Down Expand Up @@ -137,7 +142,25 @@ func askBin(ctx context.Context, path string) Commands {
defer func() {
logrus.Debugf("askBin %s=%.4f s", path, time.Since(start).Seconds())
}()

commands := Commands{}
defer func() {
addToCache(path, cachedPlugin{
Commands: commands,
})
}()
if cp, ok := findInCache(path); ok {
s := sum(path)
if s == cp.CheckSum {
logrus.Debugf("cache hit: %s", path)
commands = cp.Commands
return commands
}
}
logrus.Debugf("cache miss: %s", path)
if strings.HasPrefix(filepath.Base(path), "buffalo-no-sqlite") {
return commands
}

cmd := exec.CommandContext(ctx, path, "available")
bb := &bytes.Buffer{}
Expand Down

0 comments on commit 7a4c4d3

Please sign in to comment.