Ver Fonte

chore: cache nfs file handles

Tienson Qin há 5 anos atrás
pai
commit
b9cf077b72
2 ficheiros alterados com 21 adições e 5 exclusões
  1. 13 2
      src/main/frontend/fs.cljs
  2. 8 3
      src/main/frontend/handler/web/nfs.cljs

+ 13 - 2
src/main/frontend/fs.cljs

@@ -73,6 +73,14 @@
   ([dir path option]
    (js/window.pfs.readFile (str dir "/" path) option)))
 
+(defonce nfs-file-handles-cache (atom {}))
+(defn get-nfs-file-handle
+  [handle-path]
+  (get @nfs-file-handles-cache handle-path))
+(defn add-nfs-file-handle!
+  [handle-path handle]
+  (swap! nfs-file-handles-cache assoc handle-path handle))
+
 (defn write-file
   [dir path content]
   (cond
@@ -86,8 +94,11 @@
                            (subs dir 1)
                            (if sub-dir
                              (str "/" sub-dir)))
-          basename-handle-path (str handle-path "/" basename)]
-      (p/let [file-handle (idb/get-item basename-handle-path)]
+          basename-handle-path (str handle-path "/" basename)
+          file-handle-cache (get-nfs-file-handle basename-handle-path)]
+      (p/let [file-handle (or file-handle-cache (idb/get-item basename-handle-path))]
+        (when-not file-handle-cache
+          (add-nfs-file-handle! basename-handle-path file-handle))
         (if file-handle
           (utils/writeFile file-handle content)
           ;; create file handle

+ 8 - 3
src/main/frontend/handler/web/nfs.cljs

@@ -11,6 +11,7 @@
             [frontend.state :as state]
             [clojure.string :as string]
             [frontend.ui :as ui]
+            [frontend.fs :as fs]
             [frontend.config :as config]))
 
 (defn ls-dir-files
@@ -20,7 +21,9 @@
            root-handle (nth result 0)
            dir-name (gobj/get root-handle "name")
            repo (str config/local-db-prefix dir-name)
-           _ (idb/set-item! (str "handle-" repo) root-handle)
+           root-handle-path (str "handle-" repo)
+           _ (idb/set-item! root-handle-path root-handle)
+           _ (fs/add-nfs-file-handle! root-handle-path root-handle)
            result (nth result 1)
            result (flatten (bean/->clj result))
            files (doall
@@ -35,8 +38,10 @@
                             :file/handle handle})) result))
            text-files (filter (fn [file] (contains? #{"org" "md" "markdown"} (util/get-file-ext (:file/path file)))) files)]
      (doseq [file text-files]
-       (idb/set-item! (str "handle-" repo "/" (:file/path file))
-                      (:file/handle file)))
+       (let [handle-path (str "handle-" repo "/" (:file/path file))
+             handle (:file/handle file)]
+         (idb/set-item! handle-path handle)
+         (fs/add-nfs-file-handle! handle-path handle)))
      (-> (p/all (map (fn [file]
                        (p/let [content (.text (:file/file file))]
                          (assoc file :file/content content))) text-files))