From 0cb05d17c806fb268343dcf932e355dba13cfb3c Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Tue, 11 Nov 2014 12:45:06 -0500 Subject: Convenience argument for `cache-get' Allows for setting the cache if it does not exist. --- sx-cache.el | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'sx-cache.el') diff --git a/sx-cache.el b/sx-cache.el index 098c292..e3b356b 100644 --- a/sx-cache.el +++ b/sx-cache.el @@ -38,18 +38,21 @@ (concat (symbol-name filename) ".el") sx-cache-directory)) -(defun sx-cache-get (cache) +(defun sx-cache-get (cache &optional form) "Return the data within CACHE. +If CACHE does not exist, evaluate FORM and set it to its return. + As with `sx-cache-set', CACHE is a file name within the context of `sx-cache-directory'." (unless (file-exists-p sx-cache-directory) (mkdir sx-cache-directory)) (let ((file (sx-cache-get-file-name cache))) - (when (file-exists-p file) - (with-temp-buffer - (insert-file-contents (sx-cache-get-file-name cache)) - (read (buffer-string)))))) + (if (file-exists-p file) + (with-temp-buffer + (insert-file-contents (sx-cache-get-file-name cache)) + (read (buffer-string))) + (sx-cache-set cache (eval form))))) (defun sx-cache-set (cache data) "Set the content of CACHE to DATA. -- cgit v1.2.3