2022-07-30 21:49:00 +02:00
|
|
|
;;; lisp/lib/cache.el -*- lexical-binding: t; -*-
|
2020-05-02 00:13:05 -04:00
|
|
|
|
|
|
|
;; This little library abstracts the process of writing arbitrary elisp values
|
|
|
|
;; to a 2-tiered file store (in `doom-store-dir'/`doom-store-location').
|
|
|
|
|
2020-05-02 19:26:34 -04:00
|
|
|
(defvar doom-store-dir (concat doom-etc-dir "store/")
|
2020-05-02 00:13:05 -04:00
|
|
|
"Directory to look for and store data accessed through this API.")
|
|
|
|
|
2021-03-05 18:46:05 -05:00
|
|
|
(defvar doom-store-persist-alist ()
|
2020-05-02 00:13:05 -04:00
|
|
|
"An alist of alists, containing lists of variables for the doom cache library
|
|
|
|
to persist across Emacs sessions.")
|
|
|
|
|
|
|
|
(defvar doom-store-location "default"
|
|
|
|
"The default location for cache files. This symbol is translated into a file
|
|
|
|
name under `pcache-directory' (by default a subdirectory under
|
|
|
|
`doom-store-dir'). One file may contain multiple cache entries.")
|
|
|
|
|
2020-05-02 12:51:07 -04:00
|
|
|
(defvar doom--store-table (make-hash-table :test 'equal))
|
|
|
|
|
2020-05-02 00:13:05 -04:00
|
|
|
(defun doom-save-persistent-store-h ()
|
2021-03-05 18:46:05 -05:00
|
|
|
"Hook to persist `doom-store's storage when Emacs is killed."
|
2020-05-02 19:26:34 -04:00
|
|
|
(let (locations)
|
2021-03-05 18:46:05 -05:00
|
|
|
;; Persist `doom-store-persist-alist'
|
|
|
|
(dolist (alist (butlast doom-store-persist-alist 1))
|
|
|
|
(cl-loop with location = (car alist)
|
|
|
|
for var in (cdr alist)
|
|
|
|
do (doom-store-put var (symbol-value var) nil location 'noflush)
|
|
|
|
and do (cl-pushnew location locations :test #'equal)))
|
|
|
|
;; Clean up expired entries,
|
|
|
|
(dolist (location (doom-files-in doom-store-dir :relative-to doom-store-dir))
|
|
|
|
(maphash (lambda (key val)
|
|
|
|
(when (doom--store-expired-p key val)
|
|
|
|
(cl-pushnew location locations :test #'equal)
|
|
|
|
(doom--store-rem key location 'noflush)))
|
|
|
|
(doom--store-init location)))
|
2020-05-02 19:26:34 -04:00
|
|
|
(mapc #'doom--store-flush locations)))
|
2020-05-02 00:13:05 -04:00
|
|
|
(add-hook 'kill-emacs-hook #'doom-save-persistent-store-h)
|
|
|
|
|
|
|
|
|
|
|
|
;;
|
2021-03-11 17:49:30 -05:00
|
|
|
;;; Library
|
2020-05-02 00:13:05 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-store-persist (location variables)
|
|
|
|
"Persist VARIABLES (list of symbols) in LOCATION (symbol).
|
|
|
|
This populates these variables with cached values, if one exists, and saves them
|
2020-05-02 19:26:34 -04:00
|
|
|
to file when Emacs quits. This cannot persist buffer-local variables."
|
2021-03-11 17:49:30 -05:00
|
|
|
(cl-check-type location string)
|
2020-05-02 00:13:05 -04:00
|
|
|
(dolist (var variables)
|
2020-05-02 19:26:34 -04:00
|
|
|
(when (doom-store-member-p var location)
|
2020-05-02 00:13:05 -04:00
|
|
|
(set var (doom-store-get var location))))
|
|
|
|
(setf (alist-get location doom-store-persist-alist)
|
2020-05-02 19:26:34 -04:00
|
|
|
(append variables (alist-get location doom-store-persist-alist))))
|
2020-05-02 00:13:05 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-store-desist (location &optional variables)
|
2020-05-02 19:26:34 -04:00
|
|
|
"Unregisters VARIABLES (list of symbols) in LOCATION (symbol).
|
|
|
|
Variables to persist are recorded in `doom-store-persist-alist'. Does not affect
|
|
|
|
the actual variables themselves or their values."
|
2021-03-11 17:49:30 -05:00
|
|
|
(cl-check-type location string)
|
2020-05-02 00:13:05 -04:00
|
|
|
(if variables
|
|
|
|
(setf (alist-get location doom-store-persist-alist)
|
|
|
|
(cl-set-difference (cdr (assq location doom-store-persist-alist))
|
|
|
|
variables))
|
2020-05-02 19:26:34 -04:00
|
|
|
(delq! location doom-store-persist-alist 'assoc)))
|
2020-05-02 00:13:05 -04:00
|
|
|
|
2021-03-11 17:49:30 -05:00
|
|
|
(defun doom--store-init (&optional location)
|
2021-03-05 18:46:05 -05:00
|
|
|
(cl-check-type location (or null string))
|
2021-03-11 17:49:30 -05:00
|
|
|
(let ((location (or location doom-store-location)))
|
|
|
|
(or (gethash location doom--store-table)
|
|
|
|
(let* ((file-name-handler-alist nil)
|
|
|
|
(location-path (expand-file-name location doom-store-dir)))
|
|
|
|
(if (file-exists-p location-path)
|
|
|
|
(puthash location
|
|
|
|
(with-temp-buffer
|
|
|
|
(set-buffer-multibyte nil)
|
|
|
|
(setq buffer-file-coding-system 'binary)
|
|
|
|
(insert-file-contents-literally location-path)
|
|
|
|
(read (current-buffer)))
|
|
|
|
doom--store-table)
|
|
|
|
(puthash location (make-hash-table :test 'equal)
|
|
|
|
doom--store-table))))))
|
2020-05-02 12:51:07 -04:00
|
|
|
|
2021-03-05 18:46:05 -05:00
|
|
|
(defun doom--store-expired-p (key data)
|
|
|
|
(let ((ttl (car data)))
|
|
|
|
(cond ((functionp ttl)
|
|
|
|
(not (funcall ttl key data)))
|
2021-03-11 17:55:29 -05:00
|
|
|
((consp ttl)
|
2021-03-05 18:46:05 -05:00
|
|
|
(time-less-p ttl (current-time))))))
|
2020-05-02 19:26:34 -04:00
|
|
|
|
|
|
|
(defun doom--store-flush (location)
|
2021-03-05 18:46:05 -05:00
|
|
|
"Write `doom--store-table' to `doom-store-dir'."
|
|
|
|
(let ((file-name-handler-alist nil)
|
|
|
|
(coding-system-for-write 'binary)
|
|
|
|
(write-region-annotate-functions nil)
|
2021-03-11 17:49:30 -05:00
|
|
|
(write-region-post-annotation-function nil))
|
|
|
|
(let* ((location (or location doom-store-location))
|
|
|
|
(data (doom--store-init location)))
|
|
|
|
(make-directory doom-store-dir 'parents)
|
|
|
|
(with-temp-file (expand-file-name location doom-store-dir)
|
|
|
|
(prin1 data (current-buffer)))
|
|
|
|
data)))
|
2020-05-02 00:13:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;;###autoload
|
2021-03-05 18:46:05 -05:00
|
|
|
(defun doom-store-get (key &optional location default-value noflush)
|
2020-05-02 19:26:34 -04:00
|
|
|
"Retrieve KEY from LOCATION (defaults to `doom-store-location').
|
|
|
|
If it doesn't exist or has expired, DEFAULT_VALUE is returned."
|
2021-03-11 17:49:30 -05:00
|
|
|
(let ((data (gethash key (doom--store-init location) default-value)))
|
2021-03-09 14:44:24 -05:00
|
|
|
(if (not (or (eq data default-value)
|
|
|
|
(doom--store-expired-p key data)))
|
2021-03-05 18:46:05 -05:00
|
|
|
(cdr data)
|
|
|
|
(doom-store-rem key location noflush)
|
|
|
|
default-value)))
|
2020-05-02 00:13:05 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
2021-03-05 18:46:05 -05:00
|
|
|
(defun doom-store-put (key value &optional ttl location noflush)
|
2020-05-02 19:26:34 -04:00
|
|
|
"Set KEY to VALUE in the store at LOCATION.
|
2020-10-23 14:40:10 +02:00
|
|
|
KEY can be any lisp object that is comparable with `equal'. TTL is the duration
|
|
|
|
(in seconds) after which this cache entry expires; if nil, no cache expiration.
|
|
|
|
LOCATION is the super-key to store this cache item under. It defaults to
|
|
|
|
`doom-store-location'."
|
2021-03-05 18:46:05 -05:00
|
|
|
(cl-check-type ttl (or null integer function))
|
2021-03-11 17:49:30 -05:00
|
|
|
(puthash key (cons (if (integerp ttl)
|
|
|
|
(time-add (current-time) ttl)
|
|
|
|
ttl)
|
|
|
|
value)
|
|
|
|
(doom--store-init location))
|
|
|
|
(unless noflush
|
|
|
|
(doom--store-flush location)))
|
2020-05-02 00:13:05 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
2021-03-05 18:46:05 -05:00
|
|
|
(defun doom-store-rem (key &optional location noflush)
|
2020-05-02 19:26:34 -04:00
|
|
|
"Clear a cache LOCATION (defaults to `doom-store-location')."
|
2021-03-11 17:49:30 -05:00
|
|
|
(remhash key (doom--store-init location))
|
|
|
|
(unless noflush
|
|
|
|
(doom--store-flush location)))
|
2020-05-02 19:26:34 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-store-member-p (key &optional location)
|
|
|
|
"Return t if KEY in LOCATION exists.
|
|
|
|
LOCATION defaults to `doom-store-location'."
|
|
|
|
(let ((nil-value (format "--nilvalue%s--" (current-time))))
|
|
|
|
(not (equal (doom-store-get key location nil-value)
|
|
|
|
nil-value))))
|
2020-05-02 00:13:05 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom-store-clear (&optional location)
|
2020-05-02 19:26:34 -04:00
|
|
|
"Clear the store at LOCATION (defaults to `doom-store-location')."
|
|
|
|
(let* ((location (or location doom-store-location))
|
|
|
|
(path (expand-file-name location doom-store-dir)))
|
|
|
|
(remhash location doom--store-table)
|
2020-05-02 00:13:05 -04:00
|
|
|
(when (file-exists-p path)
|
|
|
|
(delete-file path)
|
|
|
|
t)))
|