Commit aed31980560a33577a7d919f9a0c5279f85fd156

Thomas de Grivel 2014-03-03T18:10:46

Cache asset metadata.

diff --git a/asset.lisp b/asset.lisp
index 32a62b0..1f3a45e 100644
--- a/asset.lisp
+++ b/asset.lisp
@@ -32,7 +32,10 @@
 	       :type extension)
    (sources :initarg :sources
 	    :reader asset-sources
-	    :type list)))
+	    :type list)
+   (path :type string)
+   (source-path :type string)
+   (url :type string)))
 
 (defgeneric asset-ext (asset))
 (defgeneric asset-url (asset))
@@ -52,18 +55,26 @@
   (mime-type (asset-ext asset)))
 
 (defmethod asset-url ((asset asset))
-  (expand-uri nil *assets-url-template*
-	      :name (asset-name asset)
-	      :ext (subseq (string-downcase (asset-ext asset)) 1)))
+  (if (slot-boundp asset 'url)
+      #1=(slot-value asset 'url)
+      (setf #1# (expand-uri nil *assets-url-template*
+			    :name (asset-name asset)
+			    :ext (subseq (string-downcase (asset-ext asset))
+					 1)))))
 
 (defmethod asset-path ((asset asset))
-  (expand-uri nil *assets-path-template*
-	      :name (asset-name asset)
-	      :ext (subseq (string-downcase (asset-ext asset)) 1)))
+  (if (slot-boundp asset 'path)
+      #1=(slot-value asset 'path)
+      (setf #1# (expand-uri nil *assets-path-template*
+			    :name (asset-name asset)
+			    :ext (subseq (string-downcase (asset-ext asset))
+					 1)))))
 
 (defmethod asset-source-path ((asset asset))
-  (with-slots (name source-dir source-ext) asset
-    (str source-dir name source-ext)))
+  (if (slot-boundp asset 'source-path)
+      #1=(slot-value asset 'source-path)
+      (setf #1# (with-slots (name source-dir source-ext) asset
+		  (str source-dir name source-ext)))))
 
 (defmethod print-object ((asset asset) stream)
   (print-unreadable-object (asset stream :type t)
diff --git a/find.lisp b/find.lisp
index 838633d..bd27adf 100644
--- a/find.lisp
+++ b/find.lisp
@@ -140,8 +140,19 @@
 	  specs
 	  :initial-value assets))
 
+;;  Asset cache
+
+(defvar *asset-cache* (make-hash-table :test 'equal))
+
+(defun clear-asset-cache ()
+  (clrhash *asset-cache*))
+
+(defmacro asset-cache-get (key)
+  `(gethash ,key *asset-cache*))
+
 (defun find-asset (spec &optional class)
-  (first (find-assets-from-spec spec class)))
+  (or #1=(asset-cache-get `(find-asset ,spec ,class))
+      (setf #1# (first (find-assets-from-spec spec class)))))
 
 ;;  Now that we can find an asset from a string spec we can also
 ;;  add some sugar coating to asset methods.
diff --git a/package.lisp b/package.lisp
index aee5eca..9d30f46 100644
--- a/package.lisp
+++ b/package.lisp
@@ -61,6 +61,8 @@
    #:find-assets-from-spec
    #:find-assets-from-specs
    #:locate-precompiled-assets
+   ;;  Cache
+   #:clear-asset-cache
    ;;  Rendering
    #:quote-html
    #:process-asset