Skip to content

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

:lock: Add boxID to blockIAL/docIAL cache keys, update key callers (transaction/heading/blockial) to use InBox variants, skip bare key fallback for encrypted DynamicRefTexts, cleanup on LockBox/LockAllBoxes https://github.com/siyuan-note/siyuan/issues/18034

Daniel0138fd1c7a40
diff --git a/kernel/cache/ial.go b/kernel/cache/ial.go
index e3403dee56..2ebf56ed7b 100644
--- a/kernel/cache/ial.go
+++ b/kernel/cache/ial.go
@@ -18,6 +18,7 @@ package cache
import (
"maps"
+ "sync"
"github.com/dgraph-io/ristretto"
)
@@ -27,27 +28,76 @@ var docIALCache, _ = ristretto.NewCache(&ristretto.Config{
MaxCost: 1024 * 1024 * 200,
BufferItems: 64,
})
+var docIALCacheKeys = map[string]map[string]struct{}{}
+var docIALCacheKeysMu sync.Mutex
+
+func docIALCacheKey(p, boxID string) string {
+ return boxID + "\x00" + p
+}
func PutDocIAL(p string, ial map[string]string) {
- docIALCache.Set(p, ial, 128)
+ PutDocIALInBox(p, "", ial)
+}
+
+func PutDocIALInBox(p, boxID string, ial map[string]string) {
+ key := docIALCacheKey(p, boxID)
+ docIALCache.Set(key, ial, 128)
+
+ docIALCacheKeysMu.Lock()
+ defer docIALCacheKeysMu.Unlock()
+ keys := docIALCacheKeys[p]
+ if keys == nil {
+ keys = map[string]struct{}{}
+ docIALCacheKeys[p] = keys
+ }
+ keys[key] = struct{}{}
}
func GetDocIAL(p string) (ret map[string]string) {
- ial, _ := docIALCache.Get(p)
+ return GetDocIALInBox(p, "")
+}
+
+func GetDocIALInBox(p, boxID string) (ret map[string]string) {
+ ial, _ := docIALCache.Get(docIALCacheKey(p, boxID))
if nil == ial {
return
}
-
ret = map[string]string{}
maps.Copy(ret, ial.(map[string]string))
return
}
func RemoveDocIAL(p string) {
+ docIALCacheKeysMu.Lock()
+ keys := docIALCacheKeys[p]
+ delete(docIALCacheKeys, p)
+ docIALCacheKeysMu.Unlock()
+
docIALCache.Del(p)
+ docIALCache.Del(docIALCacheKey(p, ""))
+ for key := range keys {
+ docIALCache.Del(key)
+ }
+}
+
+func RemoveDocIALInBox(p, boxID string) {
+ key := docIALCacheKey(p, boxID)
+ docIALCache.Del(key)
+
+ docIALCacheKeysMu.Lock()
+ defer docIALCacheKeysMu.Unlock()
+ if keys := docIALCacheKeys[p]; keys != nil {
+ delete(keys, key)
+ if len(keys) == 0 {
+ delete(docIALCacheKeys, p)
+ }
+ }
}
func ClearDocsIAL() {
+ docIALCacheKeysMu.Lock()
+ docIALCacheKeys = map[string]map[string]struct{}{}
+ docIALCacheKeysMu.Unlock()
docIALCache.Clear()
}
@@ -56,14 +106,37 @@ var blockIALCache, _ = ristretto.NewCache(&ristretto.Config{
MaxCost: 1024 * 1024 * 200,
BufferItems: 64,
})
+var blockIALCacheKeys = map[string]map[string]struct{}{}
+var blockIALCacheKeysMu sync.Mutex
+
+func blockIALCacheKey(id, boxID string) string {
+ return boxID + "\x00" + id
+}
func PutBlockIAL(id string, ial map[string]string) {
- // 这里存入的属性值都是反转义过的,用的是 parse.IAL2Map(),而不是 parse.IAL2MapUnEsc()
- blockIALCache.Set(id, ial, 128)
+ PutBlockIALInBox(id, "", ial)
+}
+
+func PutBlockIALInBox(id, boxID string, ial map[string]string) {
+ key := blockIALCacheKey(id, boxID)
+ blockIALCache.Set(key, ial, 128)
+
+ blockIALCacheKeysMu.Lock()
+ defer blockIALCacheKeysMu.Unlock()
+ keys := blockIALCacheKeys[id]
+ if keys == nil {
+ keys = map[string]struct{}{}
+ blockIALCacheKeys[id] = keys
+ }
+ keys[key] = struct{}{}
}
func GetBlockIAL(id string) (ret map[string]string) {
- ial, _ := blockIALCache.Get(id)
+ return GetBlockIALInBox(id, "")
+}
+
+func GetBlockIALInBox(id, boxID string) (ret map[string]string) {
+ ial, _ := blockIALCache.Get(blockIALCacheKey(id, boxID))
if nil == ial {
return
}
@@ -71,9 +144,35 @@ func GetBlockIAL(id string) (ret map[string]string) {
}
func RemoveBlockIAL(id string) {
+ blockIALCacheKeysMu.Lock()
+ keys := blockIALCacheKeys[id]
+ delete(blockIALCacheKeys, id)
+ blockIALCacheKeysMu.Unlock()
+
blockIALCache.Del(id)
+ blockIALCache.Del(blockIALCacheKey(id, ""))
+ for key := range keys {
+ blockIALCache.Del(key)
+ }
+}
+
+func RemoveBlockIALInBox(id, boxID string) {
+ key := blockIALCacheKey(id, boxID)
+ blockIALCache.Del(key)
+
+ blockIALCacheKeysMu.Lock()
+ defer blockIALCacheKeysMu.Unlock()
+ if keys := blockIALCacheKeys[id]; keys != nil {
+ delete(keys, key)
+ if len(keys) == 0 {
+ delete(blockIALCacheKeys, id)
+ }
+ }
}
func ClearBlocksIAL() {
+ blockIALCacheKeysMu.Lock()
+ blockIALCacheKeys = map[string]map[string]struct{}{}
+ blockIALCacheKeysMu.Unlock()
blockIALCache.Clear()
}
diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.go
index f3491a3fa5..38f8ae4b11 100644
--- a/kernel/filesys/tree.go
+++ b/kernel/filesys/tree.go
@@ -482,7 +482,7 @@ func removeUnescapedUnicodeNull(data []byte) (ret []byte, needFix bool) {
func afterWriteTree(tree *parse.Tree) {
docIAL := parse.IAL2Map(tree.Root.KramdownIAL)
- cache.PutDocIAL(tree.Path, docIAL)
+ cache.PutDocIALInBox(tree.Path, tree.Box, docIAL)
}
// fixTreeJSONData 订正树 JSON 数据。
diff --git a/kernel/model/blockial.go b/kernel/model/blockial.go
index 2123c32c5c..2ccd7e650d 100644
--- a/kernel/model/blockial.go
+++ b/kernel/model/blockial.go
@@ -129,7 +129,7 @@ func SetBlockReminder(id, timed string) (err error) {
return
}
IncSync()
- cache.PutBlockIAL(id, attrs)
+ cache.PutBlockIALInBox(id, tree.Box, attrs)
return
}
@@ -165,7 +165,7 @@ func BatchSetBlockAttrs(blockAttrs []map[string]any) (err error) {
return e
}
- cache.PutBlockIAL(node.ID, parse.IAL2Map(node.KramdownIAL))
+ cache.PutBlockIALInBox(node.ID, tree.Box, parse.IAL2Map(node.KramdownIAL))
pushBlockAttrs(oldAttrs, node)
nodes = append(nodes, node)
}
@@ -213,7 +213,7 @@ func setNodeAttrs(node *ast.Node, tree *parse.Tree, nameValues map[string]string
}
IncSync()
- cache.PutBlockIAL(node.ID, parse.IAL2Map(node.KramdownIAL))
+ cache.PutBlockIALInBox(node.ID, tree.Box, parse.IAL2Map(node.KramdownIAL))
pushBlockAttrs(oldAttrs, node)
@@ -257,7 +257,7 @@ func setNodeAttrsWithTx(tx *Transaction, node *ast.Node, tree *parse.Tree, nameV
tx.writeTree(tree)
IncSync()
- cache.PutBlockIAL(node.ID, parse.IAL2Map(node.KramdownIAL))
+ cache.PutBlockIALInBox(node.ID, tree.Box, parse.IAL2Map(node.KramdownIAL))
pushBlockAttrs(oldAttrs, node)
return
}
diff --git a/kernel/model/crypto.go b/kernel/model/crypto.go
index 4f5d8cc307..b004871d86 100644
--- a/kernel/model/crypto.go
+++ b/kernel/model/crypto.go
@@ -754,6 +754,8 @@ func LockBox(boxID string) {
if rmErr := os.RemoveAll(filepath.Join(util.TempDir, "export", "repo")); rmErr != nil {
logging.LogWarnf("remove export/repo dir for box [%s] failed: %s", boxID, rmErr)
}
+ // 清理动态引用锚文本缓存
+ treenode.RemoveDynamicRefTexts(boxID)
}
// LockAllBoxes 清除所有已缓存的 DEK 并删除所有加密 db 文件。退出登录或全局锁定时调用。
@@ -781,6 +783,8 @@ func LockAllBoxes() {
if rmErr := os.RemoveAll(filepath.Join(util.TempDir, "export")); rmErr != nil {
logging.LogErrorf("remove export directory failed: %s", rmErr)
}
+ // 清理所有动态引用锚文本缓存
+ treenode.ClearDynamicRefTexts()
}
// WrapNewDEK 用给定 KEK 生成随机 DEK 并包络,返回 BoxEncryption 元数据。
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 2d95bcd8eb..65f88bf217 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -115,7 +115,7 @@ func (box *Box) docIAL(p string) (ret map[string]string) {
return nil
}
- ret = cache.GetDocIAL(p)
+ ret = cache.GetDocIALInBox(p, box.ID)
if nil != ret {
return ret
}
@@ -135,7 +135,7 @@ func (box *Box) docIAL(p string) (ret map[string]string) {
box.moveCorruptedData(filePath)
return nil
}
- cache.PutDocIAL(p, ret)
+ cache.PutDocIALInBox(p, box.ID, ret)
return ret
}
diff --git a/kernel/model/heading.go b/kernel/model/heading.go
index ce71d21bd1..af71b9761a 100644
--- a/kernel/model/heading.go
+++ b/kernel/model/heading.go
@@ -64,9 +64,9 @@ func (tx *Transaction) doFoldHeading(operation *Operation) (ret *TxErr) {
tx.writeTree(tree)
IncSync()
- cache.PutBlockIAL(headingID, parse.IAL2Map(heading.KramdownIAL))
+ cache.PutBlockIALInBox(headingID, tree.Box, parse.IAL2Map(heading.KramdownIAL))
for _, child := range children {
- cache.PutBlockIAL(child.ID, parse.IAL2Map(child.KramdownIAL))
+ cache.PutBlockIALInBox(child.ID, tree.Box, parse.IAL2Map(child.KramdownIAL))
}
sql.UpsertTreeQueue(tree)
operation.RetData = childrenIDs
@@ -128,9 +128,9 @@ func (tx *Transaction) doUnfoldHeading(operation *Operation) (ret *TxErr) {
tx.writeTree(tree)
IncSync()
- cache.PutBlockIAL(headingID, parse.IAL2Map(heading.KramdownIAL))
+ cache.PutBlockIALInBox(headingID, tree.Box, parse.IAL2Map(heading.KramdownIAL))
for _, child := range children {
- cache.PutBlockIAL(child.ID, parse.IAL2Map(child.KramdownIAL))
+ cache.PutBlockIALInBox(child.ID, tree.Box, parse.IAL2Map(child.KramdownIAL))
}
sql.UpsertTreeQueue(tree)
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 797c7c5740..f84298a320 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -1543,8 +1543,8 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
if "d" == n.TextMarkBlockRefSubtype {
// 偶发编辑文档标题后引用处的动态锚文本不更新 https://github.com/siyuan-note/siyuan/issues/5891
// 使用缓存的动态锚文本强制覆盖当前块中的引用节点动态锚文本
- if dRefText, ok := treenode.DynamicRefTexts.Load(n.TextMarkBlockRefID); ok && "" != dRefText {
- n.TextMarkTextContent = dRefText.(string)
+ if dRefText := treenode.GetDynamicRefText(n.TextMarkBlockRefID, tree.Box); "" != dRefText {
+ n.TextMarkTextContent = dRefText
}
}
@@ -1583,7 +1583,7 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
treenode.MoveFoldHeading(updatedNode, oldNode)
}
- cache.PutBlockIAL(updatedNode.ID, parse.IAL2Map(updatedNode.KramdownIAL))
+ cache.PutBlockIALInBox(updatedNode.ID, tree.Box, parse.IAL2Map(updatedNode.KramdownIAL))
if ast.NodeHTMLBlock == updatedNode.Type {
content := string(updatedNode.Tokens)
@@ -1889,7 +1889,7 @@ func (tx *Transaction) doSetAttrs(operation *Operation) (ret *TxErr) {
}
tx.writeTree(tree)
- cache.PutBlockIAL(id, parse.IAL2Map(node.KramdownIAL))
+ cache.PutBlockIALInBox(id, tree.Box, parse.IAL2Map(node.KramdownIAL))
return
}
diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go
index 45102b45f2..a2635418f1 100644
--- a/kernel/treenode/node.go
+++ b/kernel/treenode/node.go
@@ -452,6 +452,10 @@ func SubTypeAbbr(n *ast.Node) string {
var DynamicRefTexts = sync.Map{}
+func dynamicRefTextsKey(defBlockID, boxID string) string {
+ return boxID + "\x00" + defBlockID
+}
+
func SetDynamicBlockRefText(blockRef *ast.Node, refText string) {
if !IsBlockRef(blockRef) {
return
@@ -474,7 +478,50 @@ func SetDynamicBlockRefText(blockRef *ast.Node, refText string) {
blockRef.TextMarkTextContent = refText
// 偶发编辑文档标题后引用处的动态锚文本不更新 https://github.com/siyuan-note/siyuan/issues/5891
- DynamicRefTexts.Store(blockRef.TextMarkBlockRefID, refText)
+ defID := blockRef.TextMarkBlockRefID
+ DynamicRefTexts.Store(defID, refText)
+ // 同时以 box-aware key 存储(如果节点有 box 上下文)
+ if blockRef.Box != "" {
+ DynamicRefTexts.Store(dynamicRefTextsKey(defID, blockRef.Box), refText)
+ }
+}
+
+func GetDynamicRefText(defBlockID, boxID string) string {
+ if boxID != "" {
+ if v, ok := DynamicRefTexts.Load(dynamicRefTextsKey(defBlockID, boxID)); ok {
+ return v.(string)
+ }
+ }
+ // 回退到无 boxID 的旧 key(兼容旧数据/无 box 上下文的调用)
+ if v, ok := DynamicRefTexts.Load(defBlockID); ok {
+ return v.(string)
+ }
+ return ""
+}
+
+// RemoveDynamicRefTexts 删除指定 box 的所有动态引用锚文本缓存。
+func RemoveDynamicRefTexts(boxID string) {
+ prefix := boxID + "\x00"
+ DynamicRefTexts.Range(func(k, _ any) bool {
+ if key, ok := k.(string); ok {
+ if strings.HasPrefix(key, prefix) {
+ DynamicRefTexts.Delete(k)
+ }
+ }
+ return true
+ })
+}
+
+func RemoveDynamicRefTextsByDefID(defBlockID string) {
+ DynamicRefTexts.Delete(defBlockID)
+}
+
+// ClearDynamicRefTexts 清空所有动态引用锚文本缓存。
+func ClearDynamicRefTexts() {
+ DynamicRefTexts.Range(func(k, _ any) bool {
+ DynamicRefTexts.Delete(k)
+ return true
+ })
}
func IsChartCodeBlockCode(code *ast.Node) bool {