add lru cache for <get-block

This commit is contained in:
Tienson Qin
2025-03-19 18:02:41 +08:00
parent 0528caabe1
commit 647e7e4584
4 changed files with 54 additions and 57 deletions

View File

@@ -2078,16 +2078,7 @@
(defn- block-content-empty?
[block]
(let [ast-title (:block.temp/ast-title block)
ast-body (:block.temp/ast-body block)
db-based? (config/db-based-graph? (state/get-current-repo))]
(and
(or db-based?
(property-file/properties-hidden? (:block/properties block)))
(empty? ast-title)
(every? #(= % ["Horizontal_Rule"]) ast-body))))
(string/blank? (:block/title block)))
(rum/defcs block-control < rum/reactive
[state config block {:keys [uuid block-id collapsed? *control-show? edit? selected?]}]
@@ -3371,8 +3362,8 @@
:else
block*)
result (merge (or (db/sub-block (:db/id block)) block*)
(select-keys block [:block/level :block.temp/top? :block.temp/bottom?]))]
result (merge block*
(db/sub-block (:db/id block)))]
(if linked-block
[block* result]
[nil result])))

View File

@@ -987,28 +987,28 @@
(rum/defc closed-value-item < rum/reactive db-mixins/query
[value {:keys [inline-text icon?]}]
(when value
(let [eid (if (de/entity? value) (:db/id value) [:block/uuid value])]
(when-let [block (db/sub-block (:db/id (db/entity eid)))]
(let [property-block? (db-property/property-created-block? block)
value' (db-property/closed-value-content block)
icon (pu/get-block-property-value block :logseq.property/icon)]
(cond
icon
(if icon?
(icon-component/icon icon {:color? true})
[:div.flex.flex-row.items-center.gap-1.h-6
(icon-component/icon icon {:color? true})
(when value'
[:span value'])])
(let [eid (if (de/entity? value) (:db/id value) [:block/uuid value])
block (or (db/sub-block (:db/id (db/entity eid))) value)
property-block? (db-property/property-created-block? block)
value' (db-property/closed-value-content block)
icon (pu/get-block-property-value block :logseq.property/icon)]
(cond
icon
(if icon?
(icon-component/icon icon {:color? true})
[:div.flex.flex-row.items-center.gap-1.h-6
(icon-component/icon icon {:color? true})
(when value'
[:span value'])])
property-block?
value'
property-block?
value'
(= type :number)
[:span.number (str value')]
(= type :number)
[:span.number (str value')]
:else
(inline-text {} :markdown (str value'))))))))
:else
(inline-text {} :markdown (str value'))))))
(rum/defc select-item
[property type value {:keys [page-cp inline-text other-position? property-position _icon?] :as opts}]
@@ -1233,7 +1233,7 @@
(concat
(->> (for [item items]
(rum/with-key (select-item property type item opts) (or (:block/uuid item) (str item))))
(interpose [:span.opacity-50.-ml-2 ","]))
(interpose [:span.opacity-50.-ml-1 ","]))
(when date?
[(property-value-date-picker block property nil {:toggle-fn toggle-fn})]))
(if date?

View File

@@ -1089,16 +1089,15 @@
[idx {:keys [full-block-ids properties]} item-render]
(let [db-id (util/nth-safe full-block-ids idx)
block (db/entity db-id)
[item set-item!] (hooks/use-state block)]
[item set-item!] (hooks/use-state block)
opts {:children? false
:properties properties
:skip-transact? true
:skip-refresh? true}]
(hooks/use-effect!
(fn []
(when (and db-id (not (:block.temp/fully-loaded? block)))
(p/let [result (db-async/<get-block
(state/get-current-repo) db-id
{:children? false
:properties properties
:skip-transact? true
:cache? false})]
(when (and db-id (not block))
(p/let [result (db-async/<get-block (state/get-current-repo) db-id opts)]
(let [block (:block result)]
(set-item! (or block (some-> (:db/id block) db/entity)))))))
[db-id])

View File

@@ -3,6 +3,7 @@
(:require [cljs-time.coerce :as tc]
[cljs-time.core :as t]
[cljs-time.format :as tf]
[cljs.cache :as cache]
[datascript.core :as d]
[frontend.config :as config]
[frontend.date :as date]
@@ -110,36 +111,44 @@
(cons default-value-id result)
result)))))
(defonce *block-cache (atom (cache/lru-cache-factory {} :threshold 1000)))
(defn <get-block
[graph name-or-uuid & {:keys [children? skip-transact? skip-refresh? cache?]
:or {children? true
cache? true}
:as opts}]
(let [name' (str name-or-uuid)
*async-queries (:db/async-queries @state/state)
async-requested? (when cache? (get @*async-queries [name' opts]))
[graph id-uuid-or-name & {:keys [children? skip-transact? skip-refresh? _properties]
:or {children? true}
:as opts}]
(let [name' (str id-uuid-or-name)
cache-key [id-uuid-or-name opts]
cached-response (when (cache/has? @*block-cache cache-key)
(reset! *block-cache (cache/hit @*block-cache cache-key))
(get @*block-cache cache-key))
e (cond
(number? name-or-uuid)
(db/entity name-or-uuid)
(number? id-uuid-or-name)
(db/entity id-uuid-or-name)
(util/uuid-string? name')
(db/entity [:block/uuid (uuid name')])
:else
(db/get-page name'))
id (or (and (:block/uuid e) (str (:block/uuid e)))
(and (util/uuid-string? name') name')
name-or-uuid)]
(if (or (:block.temp/fully-loaded? e) async-requested?)
id-uuid-or-name)]
(cond
(:block.temp/fully-loaded? e)
e
cached-response
cached-response
:else
(when-let [^Object sqlite @db-browser/*worker]
(when cache?
(swap! *async-queries assoc [name' opts] true)
(state/update-state! :db/async-query-loading (fn [s] (conj s name'))))
(state/update-state! :db/async-query-loading (fn [s] (conj s name')))
(p/let [result-str (.get-blocks sqlite graph
(ldb/write-transit-str
[{:id id :opts opts}]))
result (ldb/read-transit-str result-str)
{:keys [block children] :as result'} (first result)]
(when-not skip-transact?
(state/update-state! :db/async-query-loading (fn [s] (disj s name')))
(if skip-transact?
(reset! *block-cache (cache/miss @*block-cache cache-key result'))
(let [conn (db/get-db graph false)
block-and-children (cons block children)
affected-keys [[:frontend.worker.react/block (:db/id block)]]]
@@ -147,8 +156,6 @@
(when-not skip-refresh?
(react/refresh-affected-queries! graph affected-keys))))
(when cache?
(state/update-state! :db/async-query-loading (fn [s] (disj s name'))))
(if children?
block
result'))))))