An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:zap: Improve database view performance https://github.com/siyuan-note/siyuan/issues/17830
Daniel25e4a6490110
diff --git a/kernel/av/av.go b/kernel/av/av.goindex c67c95a5d0..e21b6d870e 100644--- a/kernel/av/av.go+++ b/kernel/av/av.go@@ -31,6 +31,7 @@ import (jsoniter "github.com/json-iterator/go""github.com/siyuan-note/filelock""github.com/siyuan-note/logging"+ "github.com/siyuan-note/siyuan/kernel/cache""github.com/siyuan-note/siyuan/kernel/util")@@ -510,10 +511,18 @@ func ParseAttributeViewByPath(avJSONPath string) (ret *AttributeView, err error)avID := filepath.Base(avJSONPath)avID = strings.TrimSuffix(avID, filepath.Ext(avID))- data, readErr := filelock.ReadFile(avJSONPath)- if nil != readErr {- logging.LogErrorf("read attribute view [%s] failed: %s", avID, readErr)- return++ var data []byte+ if cached, ok := cache.GetAVData(avID); ok {+ data = cached+ } else {+ var readErr error+ data, readErr = filelock.ReadFile(avJSONPath)+ if nil != readErr {+ logging.LogErrorf("read attribute view [%s] failed: %s", avID, readErr)+ return+ }+ cache.SetAVData(avID, data)}ret = &AttributeView{RenderedViewables: map[string]Viewable{}}@@ -644,6 +653,8 @@ func SaveAttributeView(av *AttributeView) (err error) {return}+ cache.SetAVData(av.ID, data)+if util.ExceedLargeFileWarningSize(len(data)) {msg := fmt.Sprintf(util.Langs[util.Lang][268], av.Name+" "+filepath.Base(avJSONPath), util.LargeFileWarningSize)util.PushErrMsg(msg, 7000)diff --git a/kernel/cache/av.go b/kernel/cache/av.gonew file mode 100644index 0000000000..77abaaac2d--- /dev/null+++ b/kernel/cache/av.go@@ -0,0 +1,50 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package cache++import (+ "github.com/dgraph-io/ristretto"+)++var avCache, _ = ristretto.NewCache(&ristretto.Config{+ NumCounters: 1000,+ MaxCost: 1024 * 1024 * 200,+ BufferItems: 64,+})++func GetAVData(avID string) (raw []byte, ok bool) {+ v, _ := avCache.Get(avID)+ if nil == v {+ return nil, false+ }+ return v.([]byte), true+}++func SetAVData(avID string, raw []byte) {+ if raw == nil {+ return+ }+ avCache.Set(avID, raw, int64(len(raw)))+}++func RemoveAVData(avID string) {+ avCache.Del(avID)+}++func ClearAVCache() {+ avCache.Clear()+}diff --git a/kernel/model/box.go b/kernel/model/box.goindex 07c9d2df94..5879512b8d 100644--- a/kernel/model/box.go+++ b/kernel/model/box.go@@ -792,6 +792,7 @@ func FullReindex(needResetScroll bool) {cache.ClearTreeCache()cache.ClearDocsIAL()cache.ClearBlocksIAL()+ cache.ClearAVCache()task.AppendTask(task.DatabaseIndexFull, fullReindex)task.AppendTask(task.DatabaseIndexRef, IndexRefs)diff --git a/kernel/model/history.go b/kernel/model/history.goindex 06d90ede8b..4852641eac 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -280,6 +280,7 @@ func RollbackDocHistory(historyPath string) (err error) {if copyErr := filelock.CopyNewtimes(srcAvPath, destAvPath); nil != copyErr {logging.LogErrorf("copy av [%s] failed: %s", srcAvPath, copyErr)}+ cache.RemoveAVData(avNode.AttributeViewID)}avIDs = append(avIDs, avNode.AttributeViewID)@@ -467,6 +468,7 @@ func RollbackAttributeViewHistory(historyPath string) (err error) {logging.LogErrorf("copy file [%s] to [%s] failed: %s", from, to, err)return}+ cache.RemoveAVData(strings.TrimSuffix(filepath.Base(historyPath), ".json"))IncSync()util.PushMsg(Conf.Language(102), 3000)return nildiff --git a/kernel/model/mount.go b/kernel/model/mount.goindex fb2e061890..b34f88cf23 100644--- a/kernel/model/mount.go+++ b/kernel/model/mount.go@@ -252,6 +252,7 @@ func Mount(boxID string) (alreadyMount bool, err error) {cache.ClearTreeCache()cache.ClearDocsIAL()cache.ClearBlocksIAL()+ cache.ClearAVCache()avDirPath := filepath.Join(util.WorkingDir, "guide", boxID, "storage", "av")if filelock.IsExist(avDirPath) {diff --git a/kernel/model/push_queue.go b/kernel/model/push_queue.goindex ee15613654..84c34922d7 100644--- a/kernel/model/push_queue.go+++ b/kernel/model/push_queue.go@@ -183,6 +183,7 @@ func PollPushQueue() {util.PushReloadProtyle(bt.RootID)}case "reloadAttrView":+ cache.RemoveAVData(e.ID)util.BroadcastByType("protyle", "refreshAttributeView", 0, "", map[string]any{"id": e.ID})case "reloadUI":util.ReloadUI()diff --git a/kernel/model/repository.go b/kernel/model/repository.goindex 5ff7a2e440..0913f5a325 100644--- a/kernel/model/repository.go+++ b/kernel/model/repository.go@@ -55,6 +55,7 @@ import ("github.com/siyuan-note/filelock""github.com/siyuan-note/httpclient""github.com/siyuan-note/logging"+ "github.com/siyuan-note/siyuan/kernel/cache""github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/sql""github.com/siyuan-note/siyuan/kernel/task"@@ -1892,6 +1893,10 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,if file.Path == "/snippets/conf.json" {needReloadSnippet = true}++ if strings.HasPrefix(file.Path, "/storage/av/") && strings.HasSuffix(file.Path, ".json") {+ cache.RemoveAVData(strings.TrimSuffix(filepath.Base(file.Path), ".json"))+ }}removeWidgetDirSet, unloadPluginSet, uninstallPluginSet := hashset.New(), hashset.New(), hashset.New()@@ -1942,6 +1947,10 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,if file.Path == "/snippets/conf.json" {needReloadSnippet = true}++ if strings.HasPrefix(file.Path, "/storage/av/") && strings.HasSuffix(file.Path, ".json") {+ cache.RemoveAVData(strings.TrimSuffix(filepath.Base(file.Path), ".json"))+ }}for _, upsertPetal := range mergeResult.UpsertPetals {