Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching to obsidian-list-all-files function #47

Merged
merged 6 commits into from
Jul 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions obsidian.el
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,33 @@ FILE is an Org-roam file if:
"Take relative file name F and return expanded name."
(expand-file-name f obsidian-directory))

(defvar obsidian-files-cache nil "Cache for Obsidian files.")
(defvar obsidian-cache-timestamp nil "The time when the cache was last updated.")

(defcustom obsidian-cache-expiry 3600
"The number of seconds before the Obsidian cache expires."
:type 'integer
:group 'obsidian)

(defun obsidian-list-all-files ()
"Lists all Obsidian Notes files that are not in trash.

Obsidian notes files:
- Pass the `obsidian-file-p' check"
(->> (directory-files-recursively obsidian-directory "\.*$")
(-filter #'obsidian-file-p)))
(let ((current-time (float-time)))
(when (or (not obsidian-files-cache)
(> (- current-time obsidian-cache-timestamp) obsidian-cache-expiry))
(setq obsidian-files-cache
(->> (directory-files-recursively obsidian-directory "\.*$")
(-filter #'obsidian-file-p)))
(setq obsidian-cache-timestamp current-time)))
obsidian-files-cache)

(defun obsidian-clear-cache ()
"Clears the cache."
(interactive)
(setq obsidian-files-cache nil)
(setq obsidian-cache-timestamp nil))

(defun obsidian-list-all-directories ()
"Lists all Obsidian sub folders."
Expand Down Expand Up @@ -363,6 +383,7 @@ Optional argument ARG word to complete."

In the `obsidian-inbox-directory' if set otherwise in `obsidian-directory' root."
(interactive)
(obsidian-clear-cache)
(let* ((title (read-from-minibuffer "Title: "))
(filename (s-concat obsidian-directory "/" obsidian-inbox-directory "/" title ".md"))
(clean-filename (s-replace "//" "/" filename)))
Expand Down Expand Up @@ -412,7 +433,9 @@ Argument S relative file name to clean and convert to absolute."
(let* ((all-files (->> (obsidian-list-all-files) (-map #'obsidian--file-relative-name)))
(matches (obsidian--match-files f all-files))
(file (cl-case (length matches)
(0 f)
(0 (progn
(obsidian-clear-cache)
f))
(1 (car matches))
(t
(let* ((choice (completing-read "Jump to: " matches)))
Expand Down