From da59fa2afa6dc103c028d97f59b70325178e92c6 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Fri, 12 Apr 2024 09:15:03 +0800 Subject: [PATCH] :zap: Improve marketplace loading performance Fix https://github.com/siyuan-note/siyuan/issues/10973 --- kernel/bazaar/icon.go | 9 +++++++++ kernel/bazaar/package.go | 3 +++ kernel/bazaar/plugin.go | 9 +++++++++ kernel/bazaar/template.go | 9 +++++++++ kernel/bazaar/theme.go | 9 +++++++++ kernel/bazaar/widget.go | 9 +++++++++ kernel/model/cloud_service.go | 1 + 7 files changed, 49 insertions(+) diff --git a/kernel/bazaar/icon.go b/kernel/bazaar/icon.go index ffe18eecc..94c669d51 100644 --- a/kernel/bazaar/icon.go +++ b/kernel/bazaar/icon.go @@ -51,6 +51,13 @@ func Icons() (icons []*Icon) { repo := arg.(*StageRepo) repoURL := repo.URL + if pkg, found := packageCache.Get(repoURL); found { + lock.Lock() + icons = append(icons, pkg.(*Icon)) + lock.Unlock() + return + } + icon := &Icon{} innerU := util.BazaarOSSServer + "/package/" + repoURL + "/icon.json" innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(icon).Get(innerU) @@ -91,6 +98,8 @@ func Icons() (icons []*Icon) { lock.Lock() icons = append(icons, icon) lock.Unlock() + + packageCache.SetDefault(repoURL, icon) }) for _, repo := range stageIndex.Repos { waitGroup.Add(1) diff --git a/kernel/bazaar/package.go b/kernel/bazaar/package.go index eb418177d..f96161d4b 100644 --- a/kernel/bazaar/package.go +++ b/kernel/bazaar/package.go @@ -29,6 +29,7 @@ import ( "github.com/88250/lute" "github.com/araddon/dateparse" "github.com/imroc/req/v3" + gcache "github.com/patrickmn/go-cache" "github.com/siyuan-note/filelock" "github.com/siyuan-note/httpclient" "github.com/siyuan-note/logging" @@ -680,3 +681,5 @@ func disallowDisplayBazaarPackage(pkg *Package) bool { } return false } + +var packageCache = gcache.New(6*time.Hour, 30*time.Minute) // [repoURL]*Package diff --git a/kernel/bazaar/plugin.go b/kernel/bazaar/plugin.go index 66ffe35b6..0b70157ef 100644 --- a/kernel/bazaar/plugin.go +++ b/kernel/bazaar/plugin.go @@ -55,6 +55,13 @@ func Plugins(frontend string) (plugins []*Plugin) { repo := arg.(*StageRepo) repoURL := repo.URL + if pkg, found := packageCache.Get(repoURL); found { + lock.Lock() + plugins = append(plugins, pkg.(*Plugin)) + lock.Unlock() + return + } + plugin := &Plugin{} innerU := util.BazaarOSSServer + "/package/" + repoURL + "/plugin.json" innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(plugin).Get(innerU) @@ -97,6 +104,8 @@ func Plugins(frontend string) (plugins []*Plugin) { lock.Lock() plugins = append(plugins, plugin) lock.Unlock() + + packageCache.SetDefault(repoURL, plugin) }) for _, repo := range stageIndex.Repos { waitGroup.Add(1) diff --git a/kernel/bazaar/template.go b/kernel/bazaar/template.go index a230782c1..ec5b130d8 100644 --- a/kernel/bazaar/template.go +++ b/kernel/bazaar/template.go @@ -53,6 +53,13 @@ func Templates() (templates []*Template) { repo := arg.(*StageRepo) repoURL := repo.URL + if pkg, found := packageCache.Get(repoURL); found { + lock.Lock() + templates = append(templates, pkg.(*Template)) + lock.Unlock() + return + } + template := &Template{} innerU := util.BazaarOSSServer + "/package/" + repoURL + "/template.json" innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(template).Get(innerU) @@ -93,6 +100,8 @@ func Templates() (templates []*Template) { lock.Lock() templates = append(templates, template) lock.Unlock() + + packageCache.SetDefault(repoURL, template) }) for _, repo := range stageIndex.Repos { waitGroup.Add(1) diff --git a/kernel/bazaar/theme.go b/kernel/bazaar/theme.go index 3906886c1..1a32efdaa 100644 --- a/kernel/bazaar/theme.go +++ b/kernel/bazaar/theme.go @@ -53,6 +53,13 @@ func Themes() (ret []*Theme) { repo := arg.(*StageRepo) repoURL := repo.URL + if pkg, found := packageCache.Get(repoURL); found { + lock.Lock() + ret = append(ret, pkg.(*Theme)) + lock.Unlock() + return + } + theme := &Theme{} innerU := util.BazaarOSSServer + "/package/" + repoURL + "/theme.json" innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(theme).Get(innerU) @@ -93,6 +100,8 @@ func Themes() (ret []*Theme) { lock.Lock() ret = append(ret, theme) lock.Unlock() + + packageCache.SetDefault(repoURL, theme) }) for _, repo := range stageIndex.Repos { waitGroup.Add(1) diff --git a/kernel/bazaar/widget.go b/kernel/bazaar/widget.go index 55643dc6d..1d6d64e5d 100644 --- a/kernel/bazaar/widget.go +++ b/kernel/bazaar/widget.go @@ -53,6 +53,13 @@ func Widgets() (widgets []*Widget) { repo := arg.(*StageRepo) repoURL := repo.URL + if pkg, found := packageCache.Get(repoURL); found { + lock.Lock() + widgets = append(widgets, pkg.(*Widget)) + lock.Unlock() + return + } + widget := &Widget{} innerU := util.BazaarOSSServer + "/package/" + repoURL + "/widget.json" innerResp, innerErr := httpclient.NewBrowserRequest().SetSuccessResult(widget).Get(innerU) @@ -93,6 +100,8 @@ func Widgets() (widgets []*Widget) { lock.Lock() widgets = append(widgets, widget) lock.Unlock() + + packageCache.SetDefault(repoURL, widget) }) for _, repo := range stageIndex.Repos { waitGroup.Add(1) diff --git a/kernel/model/cloud_service.go b/kernel/model/cloud_service.go index dcbe38dd8..609254414 100644 --- a/kernel/model/cloud_service.go +++ b/kernel/model/cloud_service.go @@ -210,6 +210,7 @@ var ( ) func RefreshCheckJob() { + go util.GetRhyResult(true) // 发一次请求进行结果缓存 go refreshSubscriptionExpirationRemind() go refreshUser() go refreshAnnouncement()