 name under `pcache-directory' (by default a subdirectory under
 `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 ()
   "Hook to run when an Emacs session is killed. Saves all persisted variables
 listed in `doom-store-persist-alist' to files."

          doom-store-persist-alist)))

(defun doom--store-init (location)
  (or (gethash location doom--cache)
      (not (file-exists-p doom-store-dir))
      (let* ((store (expand-file-name location doom-store-dir))
             (data (and (file-exists-p store)
                        (with-temp-buffer
                          (set-buffer-multibyte nil)
                          (setq buffer-file-coding-system 'binary)
                          (let (file-name-handler-alist)
                            (insert-file-contents-literally store))
                          (setq data (read (current-buffer)))))))
        (puthash location data doom--cache)
        data)))

(defun doom--store-get (key location &optional ttl)
  (when-let* ((location-data (doom--store-init location))
              (data (gethash location location-data)))
    (and (or (null (car data))
             (time-less-p (time-add (current-time) ttl) (car data)))
         (cdr data))))

(defun doom--store-put (key value location &optional ttl)
  (let ((data (doom--store-init location)))
    (puthash location
             (cons (time-add (current-time) ttl)
                   (doom--store-get key location))
             data)
    (let ((coding-system-for-write 'binary)
          (write-region-annotate-functions nil)
          (write-region-post-annotation-function nil))
      (with-temp-file (expand-file-name location doom-store-dir)
        (prin1 data (current-buffer))))
    data))


(defvar doom--cache (make-hash-table :test 'equal))
;;;###autoload
(defun doom-store-get (key &optional location)
  "Retrieve KEY from LOCATION (defaults to `doom-store-location'), if it exists
and hasn't expired."
  (doom--store-get key (or location doom-store-location)))

;;;###autoload
(defun doom-store-set (key value &optional ttl location)
  "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
default is `doom-store-location'. "

    (cl-loop with key = (car alist)
             for var in (cdr alist)
             if (symbol-value var)
             do (doom-store-put var it nil key))))
(add-hook 'kill-emacs-hook #'doom-save-persistent-store-h)


