From b9cf077b7247a44ffed79c0eedea1b2f0eb61bd7 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Sun, 22 Nov 2020 01:42:27 +0800 Subject: [PATCH] chore: cache nfs file handles --- src/main/frontend/fs.cljs | 15 +++++++++++++-- src/main/frontend/handler/web/nfs.cljs | 11 ++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/frontend/fs.cljs b/src/main/frontend/fs.cljs index ab1d3207c..c032f72e9 100644 --- a/src/main/frontend/fs.cljs +++ b/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 diff --git a/src/main/frontend/handler/web/nfs.cljs b/src/main/frontend/handler/web/nfs.cljs index 4836720e2..9714e8e9e 100644 --- a/src/main/frontend/handler/web/nfs.cljs +++ b/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))