Fix doom-store library
And rename doom-store-set -> doom-store-put to be more consistent with the underlying operation.
This commit is contained in:
parent
17a66004f0
commit
29c0781916
1 changed files with 28 additions and 27 deletions
|
@ -15,6 +15,8 @@ to persist across Emacs sessions.")
|
||||||
name under `pcache-directory' (by default a subdirectory under
|
name under `pcache-directory' (by default a subdirectory under
|
||||||
`doom-store-dir'). One file may contain multiple cache entries.")
|
`doom-store-dir'). One file may contain multiple cache entries.")
|
||||||
|
|
||||||
|
(defvar doom--store-table (make-hash-table :test 'equal))
|
||||||
|
|
||||||
(defun doom-save-persistent-store-h ()
|
(defun doom-save-persistent-store-h ()
|
||||||
"Hook to run when an Emacs session is killed. Saves all persisted variables
|
"Hook to run when an Emacs session is killed. Saves all persisted variables
|
||||||
listed in `doom-store-persist-alist' to files."
|
listed in `doom-store-persist-alist' to files."
|
||||||
|
@ -22,7 +24,7 @@ listed in `doom-store-persist-alist' to files."
|
||||||
(cl-loop with key = (car alist)
|
(cl-loop with key = (car alist)
|
||||||
for var in (cdr alist)
|
for var in (cdr alist)
|
||||||
if (symbol-value var)
|
if (symbol-value var)
|
||||||
do (doom-store-set var it nil key))))
|
do (doom-store-put var it nil key))))
|
||||||
(add-hook 'kill-emacs-hook #'doom-save-persistent-store-h)
|
(add-hook 'kill-emacs-hook #'doom-save-persistent-store-h)
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,8 +58,8 @@ Does not affect the actual variables themselves or their values."
|
||||||
doom-store-persist-alist)))
|
doom-store-persist-alist)))
|
||||||
|
|
||||||
(defun doom--store-init (location)
|
(defun doom--store-init (location)
|
||||||
(or (gethash location doom--cache)
|
(or (gethash location doom--store-table)
|
||||||
(not (file-exists-p doom-store-dir))
|
(if (file-exists-p doom-store-dir)
|
||||||
(let* ((store (expand-file-name location doom-store-dir))
|
(let* ((store (expand-file-name location doom-store-dir))
|
||||||
(data (and (file-exists-p store)
|
(data (and (file-exists-p store)
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
|
@ -65,40 +67,39 @@ Does not affect the actual variables themselves or their values."
|
||||||
(setq buffer-file-coding-system 'binary)
|
(setq buffer-file-coding-system 'binary)
|
||||||
(let (file-name-handler-alist)
|
(let (file-name-handler-alist)
|
||||||
(insert-file-contents-literally store))
|
(insert-file-contents-literally store))
|
||||||
(setq data (read (current-buffer)))))))
|
(read (current-buffer))))))
|
||||||
(puthash location data doom--cache)
|
(puthash location data doom--store-table)
|
||||||
data)))
|
data)
|
||||||
|
(make-hash-table :test #'equal))))
|
||||||
|
|
||||||
(defun doom--store-get (key location &optional ttl)
|
(defun doom--store-get (key location &optional default-value)
|
||||||
(when-let* ((location-data (doom--store-init location))
|
(if-let* ((location-data (doom--store-init location))
|
||||||
(data (gethash location location-data)))
|
(data (gethash location location-data))
|
||||||
(and (or (null (car data))
|
(_ (or (null (car data))
|
||||||
(time-less-p (time-add (current-time) ttl) (car data)))
|
(not (time-less-p (car data) (current-time))))))
|
||||||
(cdr data))))
|
(cdr data)
|
||||||
|
default-value))
|
||||||
|
|
||||||
(defun doom--store-put (key value location &optional ttl)
|
(defun doom--store-put (key value location &optional ttl)
|
||||||
(let ((data (doom--store-init location)))
|
(let ((data (doom--store-init location)))
|
||||||
(puthash location
|
(puthash location (cons (if ttl (time-add (current-time) ttl)) value) data)
|
||||||
(cons (time-add (current-time) ttl)
|
|
||||||
(doom--store-get key location))
|
|
||||||
data)
|
|
||||||
(let ((coding-system-for-write 'binary)
|
(let ((coding-system-for-write 'binary)
|
||||||
(write-region-annotate-functions nil)
|
(write-region-annotate-functions nil)
|
||||||
(write-region-post-annotation-function nil))
|
(write-region-post-annotation-function nil))
|
||||||
|
(make-directory doom-store-dir 'parents)
|
||||||
(with-temp-file (expand-file-name location doom-store-dir)
|
(with-temp-file (expand-file-name location doom-store-dir)
|
||||||
(prin1 data (current-buffer))))
|
(prin1 data (current-buffer))))
|
||||||
data))
|
data))
|
||||||
|
|
||||||
|
|
||||||
(defvar doom--cache (make-hash-table :test 'equal))
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-store-get (key &optional location)
|
(defun doom-store-get (key &optional location default-value)
|
||||||
"Retrieve KEY from LOCATION (defaults to `doom-store-location'), if it exists
|
"Retrieve KEY from LOCATION (defaults to `doom-store-location'), if it exists
|
||||||
and hasn't expired."
|
and hasn't expired."
|
||||||
(doom--store-get key (or location doom-store-location)))
|
(doom--store-get key (or location doom-store-location) default-value))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-store-set (key value &optional ttl location)
|
(defun doom-store-put (key value &optional ttl location)
|
||||||
"Set KEY to VALUE in the cache. TTL is the time (in seconds) until this cache
|
"Set KEY to VALUE in the cache. TTL is the time (in seconds) until this cache
|
||||||
entry expires. LOCATION is the super-key to store this cache item under; the
|
entry expires. LOCATION is the super-key to store this cache item under; the
|
||||||
default is `doom-store-location'. "
|
default is `doom-store-location'. "
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue