cache.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SiYuan - Refactor your thinking
  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. "time"
  19. "github.com/88250/lute/ast"
  20. "github.com/88250/lute/parse"
  21. "github.com/dgraph-io/ristretto"
  22. "github.com/jinzhu/copier"
  23. gcache "github.com/patrickmn/go-cache"
  24. "github.com/siyuan-note/logging"
  25. )
  26. var cacheDisabled = true
  27. func enableCache() {
  28. cacheDisabled = false
  29. }
  30. func disableCache() {
  31. cacheDisabled = true
  32. }
  33. var blockCache, _ = ristretto.NewCache(&ristretto.Config{
  34. NumCounters: 102400,
  35. MaxCost: 10240,
  36. BufferItems: 64,
  37. })
  38. func ClearCache() {
  39. blockCache.Clear()
  40. }
  41. func putBlockCache(block *Block) {
  42. if cacheDisabled {
  43. return
  44. }
  45. cloned := &Block{}
  46. if err := copier.Copy(cloned, block); nil != err {
  47. logging.LogErrorf("clone block failed: %v", err)
  48. return
  49. }
  50. blockCache.Set(cloned.ID, cloned, 1)
  51. }
  52. func getBlockCache(id string) (ret *Block) {
  53. if cacheDisabled {
  54. return
  55. }
  56. b, _ := blockCache.Get(id)
  57. if nil != b {
  58. ret = b.(*Block)
  59. }
  60. return
  61. }
  62. func removeBlockCache(id string) {
  63. blockCache.Del(id)
  64. removeRefCacheByDefID(id)
  65. }
  66. var defIDRefsCache = gcache.New(30*time.Minute, 5*time.Minute) // [defBlockID]map[refBlockID]*Ref
  67. func GetRefsCacheByDefID(defID string) (ret []*Ref) {
  68. for defBlockID, refs := range defIDRefsCache.Items() {
  69. if defBlockID == defID {
  70. for _, ref := range refs.Object.(map[string]*Ref) {
  71. ret = append(ret, ref)
  72. }
  73. }
  74. }
  75. if 1 > len(ret) {
  76. ret = QueryRefsByDefID(defID, false)
  77. for _, ref := range ret {
  78. putRefCache(ref)
  79. }
  80. }
  81. return
  82. }
  83. func CacheRef(tree *parse.Tree, refNode *ast.Node) {
  84. ref := buildRef(tree, refNode)
  85. putRefCache(ref)
  86. }
  87. func putRefCache(ref *Ref) {
  88. defBlockRefs, ok := defIDRefsCache.Get(ref.DefBlockID)
  89. if !ok {
  90. defBlockRefs = map[string]*Ref{}
  91. }
  92. defBlockRefs.(map[string]*Ref)[ref.BlockID] = ref
  93. defIDRefsCache.SetDefault(ref.DefBlockID, defBlockRefs)
  94. }
  95. func removeRefCacheByDefID(defID string) {
  96. defIDRefsCache.Delete(defID)
  97. }