-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpcache.el
260 lines (217 loc) · 9.12 KB
/
pcache.el
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
;;; pcache.el --- persistent caching for Emacs. -*- lexical-binding: t; -*-
;; Copyright (C) 2011-2020 Yann Hodique
;; Author: Yann Hodique <[email protected]>
;; Keywords: extensions
;; Version: 0.5.1
;; Package-Requires: ((emacs "25.1"))
;; This file 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 2, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; pcache provides a persistent way of caching data, in a hashtable-like
;; structure. It relies on `eieio-persistent' in the backend, so that any
;; object that can be serialized by EIEIO can be stored with pcache.
;; pcache handles objects called "repositories" (`pcache-repository') and
;; "entries" (`pcache-entry'). Each repository is identified by a unique name,
;; that defines an entry in `pcache-directory'. Subdirectories are allowed, by
;; the use of a directory separator in the repository name.
;; Example:
;; (let ((repo (pcache-repository "plop")))
;; (pcache-put repo 'foo 42) ; store value 42 with key 'foo
;; (pcache-get repo 'foo) ; => 42
;; )
;; Keys can be pretty much any Lisp object, and are compared for equality using
;; `eql'
;; Optionally, cache entries can expire:
;; (let ((repo (pcache-repository "plop")))
;; (pcache-put repo 'foo 42 1) ; store value 42 with key 'foo for 1 second
;; (sleep-for 1)
;; (pcache-get repo 'foo) ; => nil
;; )
;;; Code:
(require 'cl-lib)
(require 'cl-extra)
(require 'eieio)
(require 'eieio-base)
(defvar pcache-directory (concat user-emacs-directory "var/pcache/"))
(defvar *pcache-repositories* (make-hash-table :test 'equal))
(defvar pcache-avoid-recursion nil)
(defconst pcache-default-save-delay 300)
(defconst pcache-internal-version-constant "0.5")
(defconst pcache-version-constant
(format "%s/%s" emacs-version pcache-internal-version-constant))
(defclass pcache-repository (eieio-persistent eieio-named)
((version :initarg :version :initform nil)
(version-constant :allocation :class)
(entries :initarg :entries :initform (make-hash-table))
(entry-cls :initarg :entry-cls :initform pcache-entry)
(timestamp :initarg :timestamp :initform (float-time (current-time)))
(save-delay :initarg :save-delay)))
(oset-default 'pcache-repository :save-delay pcache-default-save-delay)
(oset-default 'pcache-repository version-constant pcache-version-constant)
(defvar *pcache-repository-name* nil)
(cl-defmethod make-instance ((cls (subclass pcache-repository)) &rest args)
(let* ((newname (or (and (stringp (car args)) (car args))
(plist-get args :object-name)
*pcache-repository-name*
(symbol-name cls)))
(e (gethash newname *pcache-repositories*))
(path (concat pcache-directory newname)))
(setq args (append args (list :object-name newname)))
(or e
(and (not pcache-avoid-recursion)
(file-exists-p path)
(condition-case nil
(let* ((pcache-avoid-recursion t)
(*pcache-repository-name* newname)
(obj (eieio-persistent-read path 'pcache-repository t)))
(and (or (pcache-validate-repo obj)
(error "wrong version"))
(puthash newname obj *pcache-repositories*)
obj))
(error nil)))
(let ((obj (cl-call-next-method))
(dir (file-name-directory path)))
(unless (file-exists-p dir)
(make-directory dir t))
(oset obj :file path)
(oset obj :version (oref-default obj version-constant))
(puthash newname obj *pcache-repositories*)
obj))))
(defun pcache-hash-table-values (h)
(let (values)
(maphash (lambda (k v) (push v values)) h)
values))
;; force custom implementation.
(cl-defmethod pcache-validate-repo ((cache t))
nil)
(cl-defmethod pcache-validate-repo ((cache pcache-repository))
(and
(equal (oref cache version)
(oref-default (eieio-object-class cache) version-constant))
(hash-table-p (oref cache entries))
(cl-every
(function
(lambda (entry)
(and (object-of-class-p entry (oref cache entry-cls))
(pcache-validate-entry entry))))
(pcache-hash-table-values (oref cache entries)))))
(defclass pcache-entry ()
((timestamp :initarg :timestamp
:initform (float-time (current-time)))
(ttl :initarg :ttl :initform nil)
(value :initarg :value :initform nil)
(value-cls :initarg :value-cls :initform nil)))
;; force custom implementation.
(cl-defmethod pcache-validate-entry ((entry t))
nil)
(cl-defmethod pcache-validate-entry ((entry pcache-entry))
(or (null (oref entry value-cls))
(object-of-class-p
(oref entry value) (oref entry value-cls))))
(cl-defmethod pcache-entry-valid-p ((entry pcache-entry))
(let ((ttl (oref entry ttl)))
(or (null ttl)
(let ((time (float-time (current-time))))
(< time (+ ttl (oref entry timestamp)))))))
(cl-defmethod pcache-get ((cache pcache-repository) key &optional default)
(let* ((table (oref cache entries))
(entry (gethash key table)))
(if entry
(if (pcache-entry-valid-p entry)
(oref entry value)
(remhash key table)
default)
default)))
(cl-defmethod pcache-has ((cache pcache-repository) key)
(let* ((default (make-symbol ":nil"))
(table (oref cache entries))
(entry (gethash key table default)))
(if (eq entry default) nil
(if (pcache-entry-valid-p entry)
t nil))))
(cl-defmethod pcache-put ((cache pcache-repository) key value &optional ttl)
(let ((table (oref cache entries))
(entry (or (and (eieio-object-p value)
(object-of-class-p value 'pcache-entry)
value)
(make-instance
(oref cache entry-cls)
:value value
:value-cls (and (eieio-object-p value) (eieio-object-class value))))))
(when ttl
(oset entry :ttl ttl))
(prog1
(puthash key entry table)
(pcache-save cache))))
(cl-defmethod pcache-invalidate ((cache pcache-repository) key)
(let ((table (oref cache entries)))
(remhash key table)
(pcache-save cache)))
(cl-defmethod pcache-clear ((cache pcache-repository))
(let* ((entries (oref cache entries))
(test (hash-table-test entries))
(resize (hash-table-rehash-size entries))
(threshold (hash-table-rehash-threshold entries))
(weakness (hash-table-weakness entries)))
(oset cache :entries (make-hash-table :test test :rehash-size resize
:rehash-threshold threshold
:weakness weakness)))
(pcache-save cache))
(cl-defmethod pcache-purge-invalid ((cache pcache-repository))
(let ((table (oref cache entries)))
(maphash (lambda (k e)
(unless (pcache-entry-valid-p e)
(remhash k table)))
table)
(pcache-save cache)))
(cl-defmethod pcache-save ((cache pcache-repository) &optional force)
(let ((timestamp (oref cache timestamp))
(delay (oref cache save-delay))
(time (float-time (current-time))))
(when (or force (> time (+ timestamp delay)))
(oset cache :timestamp time)
;; make sure version is saved to file
(oset cache :version (oref-default (eieio-object-class cache) version-constant))
(eieio-persistent-save cache))))
(cl-defmethod pcache-map ((cache pcache-repository) func)
(let ((table (oref cache entries)))
(maphash func table)))
(defun pcache-kill-emacs-hook ()
(maphash (lambda (k v)
(condition-case nil
(pcache-purge-invalid v)
(error nil))
(condition-case nil
(pcache-save v t)
(error nil)))
*pcache-repositories*))
(defun pcache-destroy-repository (name)
(remhash name *pcache-repositories*)
(let ((fname (concat pcache-directory name)))
(when (file-exists-p fname)
(delete-file fname))))
(add-hook 'kill-emacs-hook #'pcache-kill-emacs-hook)
;; in case we reload in place, clean all repositories with invalid version
(let (to-clean)
(maphash (lambda (k v)
(condition-case nil
(unless (eql (oref v version)
pcache-version-constant)
(signal 'error nil))
(error
(setq to-clean (cons k to-clean)))))
*pcache-repositories*)
(dolist (k to-clean)
(remhash k *pcache-repositories*)))
(provide 'pcache)
;;; pcache.el ends here