add function to return cached compiled templates or caching a new one

This commit is contained in:
Gered 2014-03-02 13:53:38 -05:00
parent 4fad49fa3f
commit d38be15c27

View file

@ -45,6 +45,23 @@
true
(> file-last-modified other-timestamp))))
(defn- get-cached-compiled-template [^File file create-fn]
(let [filepath (.getPath file)
cache-and-return! (fn []
(let [new-compiled-template (create-fn file)]
(swap!
compiled-templates
assoc
filepath
{:last-modified (get-file-last-modified file)
:template new-compiled-template})
new-compiled-template))
cached (get @compiled-templates filepath)]
(if (and cached
(not (newer? file (:last-modified cached))))
(:template cached)
(cache-and-return!))))
(defn- get-compiled-template [^File file]
(if-not (.exists file)
(throw (new FileNotFoundException (str "Template file \"" file "\" not found.")))