blob: 098c292c61a44b0f527938fd3790c46acbfc0e54 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
;;; sx-cache.el --- caching for stack-mode
;; Copyright (C) 2014 Sean Allred
;; Author: Sean Allred <code@seanallred.com>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; All caches are retrieved and set using symbols. The symbol should
;; be the sub-subpackage that is using the cache. For example,
;; `sx-pkg' would use `(sx-cache-get 'pkg)'.
;;
;; This symbol is then converted into a filename within
;; `sx-cache-directory'.
;;; Code:
(defcustom sx-cache-directory
(expand-file-name ".stackmode" user-emacs-directory)
"Directory containined cached files and precompiled filters.")
(defun sx-cache-get-file-name (filename)
"Expands FILENAME in the context of `sx-cache-directory'."
(expand-file-name
(concat (symbol-name filename) ".el")
sx-cache-directory))
(defun sx-cache-get (cache)
"Return the data within CACHE.
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))))))
(defun sx-cache-set (cache data)
"Set the content of CACHE to DATA.
As with `sx-cache-get', CACHE is a file name within the
context of `sx-cache-directory'.
DATA will be written as returned by `prin1'."
(unless (file-exists-p sx-cache-directory)
(mkdir sx-cache-directory))
(write-region (prin1-to-string data) nil
(sx-cache-get-file-name cache))
data)
(provide 'sx-cache)
;;; sx-cache.el ends here
;; Local Variables:
;; indent-tabs-mode: nil
;; End:
|