Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:lock: Isolate block cache by notebook #18034

Danielbb2f746f82cc
diff --git a/kernel/sql/cache.go b/kernel/sql/cache.go
index b0d5bc1630..117b08df77 100644
--- a/kernel/sql/cache.go
+++ b/kernel/sql/cache.go
@@ -18,6 +18,7 @@ package sql
import (
"strings"
+ "sync"
"time"
"github.com/88250/lute/ast"
@@ -43,10 +44,34 @@ var blockCache, _ = ristretto.NewCache(&ristretto.Config{
NumCounters: 100000,
MaxCost: 10240,
BufferItems: 64,
+ OnExit: func(value interface{}) {
+ if entry, ok := value.(*blockCacheEntry); ok {
+ removeBlockCacheKey(entry.block.ID, entry.key)
+ }
+ },
})
+var blockCacheKeys = map[string]map[string]struct{}{}
+var blockCacheKeysMu sync.Mutex
+
+type blockCacheEntry struct {
+ key string
+ block *Block
+}
+
+// blockCacheKey 为加密笔记本使用 box 维度缓存键;普通笔记本保持原有全局键,避免影响既有查询路径。
+func blockCacheKey(id, boxID string) string {
+ if IsEncryptedBoxFn != nil && IsEncryptedBoxFn(boxID) {
+ return boxID + "\x00" + id
+ }
+ return id
+}
+
func ClearCache() {
blockCache.Clear()
+ blockCacheKeysMu.Lock()
+ blockCacheKeys = map[string]map[string]struct{}{}
+ blockCacheKeysMu.Unlock()
clearRefCache()
}
@@ -62,26 +87,64 @@ func putBlockCache(block *Block) {
}
cloned.Content = strings.ReplaceAll(cloned.Content, search.SearchMarkLeft, "")
cloned.Content = strings.ReplaceAll(cloned.Content, search.SearchMarkRight, "")
- blockCache.Set(cloned.ID, cloned, 1)
+ key := blockCacheKey(cloned.ID, cloned.Box)
+ addBlockCacheKey(cloned.ID, key)
+ if !blockCache.Set(key, &blockCacheEntry{key: key, block: cloned}, 1) {
+ removeBlockCacheKey(cloned.ID, key)
+ }
}
func getBlockCache(id string) (ret *Block) {
+ return getBlockCacheInBox(id, "")
+}
+
+func getBlockCacheInBox(id, boxID string) (ret *Block) {
if cacheDisabled {
return
}
- b, _ := blockCache.Get(id)
+ b, _ := blockCache.Get(blockCacheKey(id, boxID))
if nil != b {
- ret = b.(*Block)
+ if entry, ok := b.(*blockCacheEntry); ok {
+ ret = entry.block
+ }
}
return
}
func removeBlockCache(id string) {
- blockCache.Del(id)
+ blockCacheKeysMu.Lock()
+ keys := blockCacheKeys[id]
+ delete(blockCacheKeys, id)
+ blockCacheKeysMu.Unlock()
+ for key := range keys {
+ blockCache.Del(key)
+ }
removeRefCacheByDefID(id)
}
+func addBlockCacheKey(id, key string) {
+ blockCacheKeysMu.Lock()
+ defer blockCacheKeysMu.Unlock()
+ keys := blockCacheKeys[id]
+ if keys == nil {
+ keys = map[string]struct{}{}
+ blockCacheKeys[id] = keys
+ }
+ keys[key] = struct{}{}
+}
+
+func removeBlockCacheKey(id, key string) {
+ blockCacheKeysMu.Lock()
+ defer blockCacheKeysMu.Unlock()
+ if keys := blockCacheKeys[id]; keys != nil {
+ delete(keys, key)
+ if len(keys) == 0 {
+ delete(blockCacheKeys, id)
+ }
+ }
+}
+
var defIDRefsCache = gcache.New(30*time.Minute, 5*time.Minute)
func refCacheKey(defBlockID, boxID string) string {
diff --git a/kernel/sql/cache_test.go b/kernel/sql/cache_test.go
new file mode 100644
index 0000000000..d5e6a64771
--- /dev/null
+++ b/kernel/sql/cache_test.go
@@ -0,0 +1,63 @@
+// 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 sql
+
+import "testing"
+
+// TestBlockCacheIsolatedByEncryptedBox 验证加密笔记本块不会污染全局缓存,且不同加密笔记本可安全使用相同块 ID。
+func TestBlockCacheIsolatedByEncryptedBox(t *testing.T) {
+ originalDisabled := cacheDisabled
+ originalIsEncryptedBoxFn := IsEncryptedBoxFn
+ defer func() {
+ cacheDisabled = originalDisabled
+ IsEncryptedBoxFn = originalIsEncryptedBoxFn
+ ClearCache()
+ }()
+
+ cacheDisabled = false
+ IsEncryptedBoxFn = func(boxID string) bool {
+ return boxID == "encrypted-a" || boxID == "encrypted-b"
+ }
+ ClearCache()
+
+ putBlockCache(&Block{ID: "shared-id", Box: "encrypted-a", Content: "secret-a"})
+ putBlockCache(&Block{ID: "shared-id", Box: "encrypted-b", Content: "secret-b"})
+ putBlockCache(&Block{ID: "normal-id", Box: "normal", Content: "normal"})
+ blockCache.Wait()
+
+ if block := getBlockCache("shared-id"); block != nil {
+ t.Fatalf("global cache must not return encrypted block, got box %q", block.Box)
+ }
+ if block := getBlockCacheInBox("shared-id", "encrypted-a"); block == nil || block.Content != "secret-a" {
+ t.Fatalf("encrypted-a cache miss or cross-box result: %#v", block)
+ }
+ if block := getBlockCacheInBox("shared-id", "encrypted-b"); block == nil || block.Content != "secret-b" {
+ t.Fatalf("encrypted-b cache miss or cross-box result: %#v", block)
+ }
+ if block := getBlockCache("normal-id"); block == nil || block.Content != "normal" {
+ t.Fatalf("normal block cache behavior changed: %#v", block)
+ }
+
+ removeBlockCache("shared-id")
+ blockCache.Wait()
+ if block := getBlockCacheInBox("shared-id", "encrypted-a"); block != nil {
+ t.Fatalf("encrypted-a cache entry was not removed: %#v", block)
+ }
+ if block := getBlockCacheInBox("shared-id", "encrypted-b"); block != nil {
+ t.Fatalf("encrypted-b cache entry was not removed: %#v", block)
+ }
+}
diff --git a/kernel/sql/encrypted_query.go b/kernel/sql/encrypted_query.go
index 43ad61345f..1040685fa7 100644
--- a/kernel/sql/encrypted_query.go
+++ b/kernel/sql/encrypted_query.go
@@ -31,7 +31,7 @@ import (
// GetBlockInBox 按 id 在指定 box 的 db 里查 block。boxID 为空则查全局 db。
func GetBlockInBox(id, boxID string) (ret *Block) {
- ret = getBlockCache(id)
+ ret = getBlockCacheInBox(id, boxID)
if nil != ret {
return
}