cache.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package sql
  17. import (
  18. "runtime"
  19. "time"
  20. "github.com/88250/lute/ast"
  21. "github.com/88250/lute/parse"
  22. "github.com/dgraph-io/ristretto"
  23. "github.com/jinzhu/copier"
  24. gcache "github.com/patrickmn/go-cache"
  25. "github.com/siyuan-note/logging"
  26. )
  27. var memCache, _ = ristretto.NewCache(&ristretto.Config{
  28. NumCounters: 800000,
  29. MaxCost: 1000 * 1000 * 100,
  30. BufferItems: 64,
  31. })
  32. var disabled = true
  33. func EnableCache() {
  34. disabled = false
  35. }
  36. func DisableCache() {
  37. disabled = true
  38. }
  39. func ClearBlockCache() {
  40. memCache.Clear()
  41. runtime.GC()
  42. }
  43. func putBlockCache(block *Block) {
  44. if disabled {
  45. return
  46. }
  47. cloned := &Block{}
  48. if err := copier.Copy(cloned, block); nil != err {
  49. logging.LogErrorf("clone block failed: %v", err)
  50. return
  51. }
  52. memCache.Set(cloned.ID, cloned, 1)
  53. }
  54. func getBlockCache(id string) (ret *Block) {
  55. if disabled {
  56. return
  57. }
  58. b, _ := memCache.Get(id)
  59. if nil != b {
  60. ret = b.(*Block)
  61. }
  62. return
  63. }
  64. func removeBlockCache(id string) {
  65. memCache.Del(id)
  66. removeRefCacheByDefID(id)
  67. }
  68. var virtualRefKeywordsCacheTime = time.Now()
  69. func getVirtualRefKeywordsCache() ([]string, bool) {
  70. if disabled {
  71. return nil, false
  72. }
  73. // 虚拟引用关键字缓存调整为 10 分钟 https://github.com/siyuan-note/siyuan/issues/6602
  74. if 10 < time.Now().Sub(virtualRefKeywordsCacheTime).Minutes() {
  75. ClearVirtualRefKeywords()
  76. return nil, false
  77. }
  78. if val, ok := memCache.Get("virtual_ref"); ok {
  79. return val.([]string), true
  80. }
  81. return nil, false
  82. }
  83. func setVirtualRefKeywords(keywords []string) {
  84. if disabled {
  85. return
  86. }
  87. memCache.Set("virtual_ref", keywords, 1)
  88. }
  89. func ClearVirtualRefKeywords() {
  90. memCache.Del("virtual_ref")
  91. }
  92. var defIDRefsCache = gcache.New(30*time.Minute, 5*time.Minute) // [defBlockID]map[refBlockID]*Ref
  93. func GetRefsCacheByDefID(defID string) (ret []*Ref) {
  94. for defBlockID, refs := range defIDRefsCache.Items() {
  95. if defBlockID == defID {
  96. for _, ref := range refs.Object.(map[string]*Ref) {
  97. ret = append(ret, ref)
  98. }
  99. }
  100. }
  101. if 1 > len(ret) {
  102. ret = QueryRefsByDefID(defID, false)
  103. for _, ref := range ret {
  104. putRefCache(ref)
  105. }
  106. }
  107. return
  108. }
  109. func CacheRef(tree *parse.Tree, refNode *ast.Node) {
  110. ref := buildRef(tree, refNode)
  111. putRefCache(ref)
  112. }
  113. func putRefCache(ref *Ref) {
  114. defBlockRefs, ok := defIDRefsCache.Get(ref.DefBlockID)
  115. if !ok {
  116. defBlockRefs = map[string]*Ref{}
  117. }
  118. defBlockRefs.(map[string]*Ref)[ref.BlockID] = ref
  119. defIDRefsCache.SetDefault(ref.DefBlockID, defBlockRefs)
  120. }
  121. func removeRefCacheByDefID(defID string) {
  122. defIDRefsCache.Delete(defID)
  123. }