Skip to content

Commit

Permalink
Add caching to obsidian-list-all-files function (#47)
Browse files Browse the repository at this point in the history
* Add caching to obsidian-list-all-files function

* Allow user to set cache expiry for obsidian-list-all-files

* Add after-save-hook to clear cache in obsidian-capture function

* Implement onetime cache clearing hook for new file creation

* Clear cache before creating new files in obsidian-capture and obsidian-find-file

* Clear cache only when creating new files in obsidian-find-file
  • Loading branch information
kdmsnr authored Jul 2, 2023
1 parent 0d392ec commit 3d63b9c
Showing 1 changed file with 26 additions and 3 deletions.
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

0 comments on commit 3d63b9c

Please sign in to comment.