perf: add cached :thread-api/get-blocks

This commit is contained in:
rcmerci
2025-04-02 23:56:52 +08:00
parent 59ef4e14d2
commit 2b482ee941

View File

@@ -479,13 +479,24 @@
(d/pull @conn selector)
(sqlite-common-db/with-parent @conn)))))
(def ^:private *get-blocks-cache (volatile! (cache/lru-cache-factory {} :threshold 1000)))
(def ^:private get-blocks-with-cache
(common.cache/cache-fn
*get-blocks-cache
(fn [repo requests]
(let [db (some-> (worker-state/get-datascript-conn repo) deref)]
[[repo (:max-tx db) requests]
[db requests]]))
(fn [db requests]
(when db
(mapv (fn [{:keys [id opts]}]
(let [id' (if (and (string? id) (common-util/uuid-string? id)) (uuid id) id)]
(-> (sqlite-common-db/get-block-and-children db id' opts)
(assoc :id id)))) requests)))))
(def-thread-api :thread-api/get-blocks
[repo requests]
(when-let [conn (worker-state/get-datascript-conn repo)]
(mapv (fn [{:keys [id opts]}]
(let [id' (if (and (string? id) (common-util/uuid-string? id)) (uuid id) id)]
(-> (sqlite-common-db/get-block-and-children @conn id' opts)
(assoc :id id)))) requests)))
(get-blocks-with-cache repo requests))
(def-thread-api :thread-api/get-block-refs
[repo id]