Browse Source

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

rcmerci 7 months ago
parent
commit
2b482ee941
1 changed files with 16 additions and 5 deletions
  1. 16 5
      src/main/frontend/worker/db_worker.cljs

+ 16 - 5
src/main/frontend/worker/db_worker.cljs

@@ -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]