-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcache.lisp
More file actions
30 lines (28 loc) · 1.3 KB
/
cache.lisp
File metadata and controls
30 lines (28 loc) · 1.3 KB
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
(defpackage #:inga/cache
(:use #:cl)
(:export #:defunc))
(in-package #:inga/cache)
(defmacro defunc (name params &body body)
(let ((cache-sym (gensym "CACHE-"))
(required-params (loop for p in params
with is-optional
with results
do
(when (eq p '&optional) (setf is-optional t))
(unless is-optional (push p results))
finally (return (reverse results)))))
`(progn
(defparameter ,cache-sym (make-hash-table :test 'equal))
(defun ,name ,params
(if (equal (uiop:getenv "INGA_DEBUG") "1")
(progn ,@body)
(let* ((cache-key (list ,@required-params))
(cached-result (gethash cache-key ,cache-sym)))
(if cached-result
(if (eq cached-result 'empty) nil cached-result)
(let ((result (progn ,@body)))
(setf (gethash cache-key ,cache-sym) (or result 'empty))
result)))))
(defun ,(intern (concatenate 'string "EVICTC-" (string name))) ,required-params
(unless (equal (uiop:getenv "INGA_DEBUG") "1")
(remhash (list ,@required-params) ,cache-sym))))))