From 70b68e1f413d43a775806c269ebadb38a07f3151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 8 Sep 2016 08:21:40 +0200 Subject: [PATCH] fdroidcl: cache all metadata to disk Reduces `fdroidcl` startup time if it needs the metadata (e.g. `search`) from 300ms to 30ms on my machine. Updates #12. --- cmd/fdroidcl/update.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/fdroidcl/update.go b/cmd/fdroidcl/update.go index e601245..bfab95d 100644 --- a/cmd/fdroidcl/update.go +++ b/cmd/fdroidcl/update.go @@ -6,6 +6,7 @@ package main import ( "bytes" "crypto/sha256" + "encoding/gob" "fmt" "io" "io/ioutil" @@ -132,7 +133,14 @@ func indexPath(name string) string { return filepath.Join(mustData(), name+".jar") } -func mustLoadIndexes() []fdroidcl.App { +func mustLoadIndexes() (apps []fdroidcl.App) { + cache := filepath.Join(mustData(), "cache-gob") + if f, err := os.Open(cache); err == nil { + defer f.Close() + if err := gob.NewDecoder(f).Decode(&apps); err == nil { + return + } + } m := make(map[string]*fdroidcl.App) for _, r := range config.Repos { if !r.Enabled { @@ -157,10 +165,14 @@ func mustLoadIndexes() []fdroidcl.App { m[app.ID].Apks = apks } } - apps := make([]fdroidcl.App, 0, len(m)) + apps = make([]fdroidcl.App, 0, len(m)) for _, a := range m { apps = append(apps, *a) } sort.Sort(fdroidcl.AppList(apps)) - return apps + if f, err := os.Create(cache); err == nil { + defer f.Close() + gob.NewEncoder(f).Encode(apps) + } + return }