emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Proposing two new functions in filecache.el


From: Mathias Dahl
Subject: Proposing two new functions in filecache.el
Date: Thu, 15 Dec 2005 19:27:24 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (windows-nt)

I just learned about the very usefil File Name Cache and propose the
following or similar addition to filecache.el. 

The reason is that adding remote directories is a time-consuming
operation, and for directories that seldom change, there is no point
in creating the file cache each time Emacs is restarted. So, why not
add functionality to save and read the cache from a file?

;;; Code begins here

(defun file-cache-save-cache-to-file (file)
  "Save contents of `file-cache-alist' to FILE.
For later retrieval using `file-cache-read-cache-from-file'"
  (interactive)
  (let ((buf (get-buffer-create "*file-cache*")))
    (save-excursion
      (set-buffer buf)
      (erase-buffer)
      ;; If there is some neater way of doing the saving, I am all for
      ;; it...
      (insert
       (with-output-to-string
         (print file-cache-alist)))
      (write-region (point-min) (point-max) (expand-file-name file)))))

(defun file-cache-read-cache-from-file (file)
  "Clear `file-cache-alist' and read cache from FILE.
The file cache can be saved to a file using
`file-cache-save-cache-to-file'."
  (interactive)
  (file-cache-clear-cache)
  (let ((buf (get-buffer-create "*file-cache*")))
    (save-excursion
      (set-buffer buf)
      (erase-buffer)
      ;; Similary to the read function, if there is a better way to
      ;; restore file-cache-alist...
      (insert-file-contents file)
      (setq file-cache-alist 
            (car (read-from-string 
                  (buffer-substring (point-min) (point-max))))))))

;;; Code ends here

Here is some code to test and verify that it works:

First load the cache with some file names:

  (file-cache-add-directory "~/")

Next, check size of cache:

  (message "File cache size: %d" (length file-cache-alist))

Save cache to a file:

  (file-cache-save-cache-to-file "~/.file_cache")

Clear it and make sure it is empty:

  (file-cache-clear-cache)
  (message "File cache size: %d" (length file-cache-alist))

Read cache from file and verify we got the same number of cache items back:

  (file-cache-read-cache-from-file "~/.file_cache")
  (message "File cache size: %d" (length file-cache-alist))

In your .emacs file, you only need to do this:

  (require 'filecache)
  (file-cache-read-cache-from-file "~/.file_cache")

What do others think? Would this be acceptable/useful? If not, I will
happily keep this hack to myself.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]