          (subdir (expand-file-name (substring id 0 2) root)))
     (expand-file-name id subdir)))
 
+(defun* elfeed-ref-archive-filename (&optional (suffix ""))
+  "Return the base filename of the archive files."
+  (concat (expand-file-name "data/archive" elfeed-db-directory) suffix))
+
+(defun elfeed-ref-archive-load ()
+  "Load the archived ref index."
+  (let ((archive-index (elfeed-ref-archive-filename ".index")))
+    (if (file-exists-p archive-index)
+      (with-temp-buffer
+        (insert-file-contents archive-index)
+        (setf elfeed-ref-archive (read (current-buffer))))
+      (setf elfeed-ref-archive :empty))))
+
+(defun elfeed-ref-archive-ensure ()
+  "Ensure that the archive index is loaded."
+  (when (null elfeed-ref-archive) (elfeed-ref-archive-load)))
+
 (defun elfeed-ref-exists-p (ref)
   "Return true if REF can be dereferenced."
-  (file-exists-p (elfeed-ref--file ref)))
+  (elfeed-ref-archive-ensure)
+  (or (and (hash-table-p elfeed-ref-archive)
+           (not (null (gethash (elfeed-ref-id ref) elfeed-ref-archive))))
+      (file-exists-p (elfeed-ref--file ref))))
 
 (defun elfeed-deref (ref)
   "Fetch the content behind the reference, or nil if non-existent."
+  (elfeed-ref-archive-ensure)
   (if (not (elfeed-ref-p ref))
       ref
-    (let ((file (elfeed-ref--file ref)))
-      (when (file-exists-p file)
-        (with-temp-buffer
-          (insert-file-contents file)
-          (buffer-string))))))
+    (let ((index (and (hash-table-p elfeed-ref-archive)
+                      (gethash (elfeed-ref-id ref) elfeed-ref-archive)))
+          (archive-file (elfeed-ref-archive-filename ".gz")))
+      (if (and index (file-exists-p archive-file))
+          (progn
+            (when (null elfeed-ref-cache)
+              (with-temp-buffer
+                (insert-file-contents archive-file)
+                (setf elfeed-ref-cache (buffer-string)))
+              ;; Clear cache on next turn.
+              (run-at-time 0 nil (lambda () (setf elfeed-ref-cache nil))))
+            (substring elfeed-ref-cache (car index) (cdr index)))
+        (let ((file (elfeed-ref--file ref)))
+          (when (file-exists-p file)
+            (with-temp-buffer
+              (insert-file-contents file)
+              (buffer-string))))))))
 
 (defun elfeed-ref (content)
   "Create a reference to CONTENT, to be persistently stored."


;; Filesystem storage:

(defstruct elfeed-ref
  id)


                        when (null (cddr (directory-files dir)))
                        do (delete-directory dir)))))

(defun elfeed-db-pack ()
  "Pack all content into a single archive for efficient storage."
  (let ((coding-system-for-write 'utf-8)
        (next-archive (make-hash-table :test 'equal))
        (packed ()))
    (with-temp-file (elfeed-ref-archive-filename ".gz")
      (with-elfeed-db-visit (entry _)
        (let ((ref (elfeed-entry-content entry))
              (start (1- (point))))
          (when (elfeed-ref-p ref)
            (let ((content (elfeed-deref ref)))
              (when content
                (push ref packed)
                (insert content)
                (setf (gethash (elfeed-ref-id ref) next-archive)
                      (cons start (1- (point))))))))))
    (with-temp-file (elfeed-ref-archive-filename ".index")
      (let ((standard-output (current-buffer)))
        (prin1 next-archive)))
    (setf elfeed-ref-archive next-archive)
    (mapc #'elfeed-ref-delete packed)
    :success))

(defun elfeed-db-gc-safe ()
  "Run `elfeed-db-gc' without triggering any errors, for use as a safe hook."
  (ignore-errors (elfeed-db-gc)))
