aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Allred <code@seanallred.com>2014-11-02 13:11:15 -0500
committerSean Allred <code@seanallred.com>2014-11-02 13:11:15 -0500
commitf349fe5d764730a0a339045a59a46d53b9a33100 (patch)
tree37259374736486ce6398680522f3577b659b9c17
parent73a1b008211cc2ca548c30ff99b2950c99e53325 (diff)
Implement and use `stack-cache-get' and `-set'
-rw-r--r--stack-core.el30
-rw-r--r--stack-filter.el24
2 files changed, 32 insertions, 22 deletions
diff --git a/stack-core.el b/stack-core.el
index f9b9669..c2aff15 100644
--- a/stack-core.el
+++ b/stack-core.el
@@ -249,13 +249,29 @@ entire response as a complex alist."
"Expands FILENAME in the context of `stack-cache-directory'."
(expand-file-name filename stack-cache-directory))
-(defun stack-cache-get-file (filename)
- "Return a buffer for FILENAME from `stack-cache-directory'."
- (let ((find-file-hook nil)
- (file (stack-cache-get-file-name filename)))
- (unless (file-exists-p stack-cache-directory)
- (mkdir stack-cache-directory))
- (find-file-noselect file)))
+(defun stack-cache-get (cache)
+ "Return the data within CACHE.
+
+As with `stack-cache-set', CACHE is a file name within the
+context of `stack-cache-directory'."
+ (unless (file-exists-p stack-cache-directory)
+ (mkdir stack-cache-directory))
+ (let ((file (stack-cache-get-file-name cache)))
+ (when (file-exists-p file)
+ (with-temp-buffer
+ (insert-file-contents (stack-cache-get-file-name cache))
+ (read (buffer-string))))))
+
+(defun stack-cache-set (cache data)
+ "Set the content of CACHE to DATA.
+
+As with `stack-cache-get', CACHE is a file name within the
+context of `stack-cache-directory'."
+ (unless (file-exists-p stack-cache-directory)
+ (mkdir stack-cache-directory))
+ (write-region (prin1-to-string data) nil
+ (stack-cache-get-file-name cache))
+ data)
(provide 'stack-core)
;;; stack-core.el ends here
diff --git a/stack-filter.el b/stack-filter.el
index e5fb54d..91c4875 100644
--- a/stack-filter.el
+++ b/stack-filter.el
@@ -74,9 +74,7 @@ or string."
(defun stack-filter-get (filter)
"Retrieve named FILTER from `stack-filter-cache-file'."
- (with-current-buffer (stack-cache-get-file stack-filter-cache-file)
- (or (cdr (ignore-errors (assoc filter (read (buffer-string)))))
- nil)))
+ (cdr (assoc filter (stack-cache-get stack-filter-cache-file))))
(defun stack-filter-store (name filter)
"Store NAME as FILTER in `stack-filter-cache-file'.
@@ -85,17 +83,12 @@ NAME should be a symbol and FILTER is a string as compiled by
`stack-filter-compile'."
(unless (symbolp name)
(error "Name must be a symbol: %S" name))
- (with-current-buffer (stack-cache-get-file stack-filter-cache-file)
- (let* ((dict (or (ignore-errors
- (read (buffer-string)))
- nil))
- (entry (assoc name dict)))
- (if entry (setf (cdr entry) filter)
- (setq dict (cons (cons name filter) dict)))
- (erase-buffer)
- (insert (prin1-to-string dict))
- (save-buffer)
- dict)))
+ (let* ((dict (stack-cache-get stack-filter-cache-file))
+ (entry (assoc name dict)))
+ (if entry (setcdr entry filter)
+ (setq dict (cons (cons name filter) dict)))
+
+ (stack-cache-set stack-filter-cache-file dict)))
;;; TODO tail recursion should be made more efficient for the
;;; byte-compiler
@@ -104,7 +97,8 @@ NAME should be a symbol and FILTER is a string as compiled by
(stack-filter-store
(caar name-filter-alist)
(cdar name-filter-alist))
- (stack-filter-store-all (cdr name-filter-alist))))
+ (stack-filter-store-all
+ (cdr name-filter-alist))))
(provide 'stack-filter)
;;; stack-filter.el ends here