Resource management / caching with optional automatic expiry on file modification
This commit is contained in:
parent
e3d4129d64
commit
d7e0453578
57
src/hottub/resource.clj
Normal file
57
src/hottub/resource.clj
Normal file
|
@ -0,0 +1,57 @@
|
|||
(ns hottub.resource
|
||||
(:use hottub.util)
|
||||
(:import [org.newdawn.slick Image]))
|
||||
|
||||
(def resource-cache (atom {}))
|
||||
(def garbage-resources (atom #{}))
|
||||
|
||||
(defn last-modified [filename]
|
||||
(.lastModified (java.io.File. filename)))
|
||||
|
||||
(defn cached-resource [filename]
|
||||
(:resource (get @resource-cache filename)))
|
||||
|
||||
(defn cache-resource [filename resource fncleanup]
|
||||
(println "caching new resource" filename)
|
||||
(swap! resource-cache assoc filename
|
||||
{:resource resource
|
||||
:last-modified (last-modified filename)
|
||||
:fncleanup fncleanup})
|
||||
resource)
|
||||
|
||||
(defn expire-resource [filename]
|
||||
(swap! garbage-resources conj filename))
|
||||
|
||||
(defn- expire-resource-impl [filename]
|
||||
(if-let [resource (get @resource-cache filename)]
|
||||
(do
|
||||
(swap! resource-cache dissoc filename)
|
||||
(if-let [fncleanup (:fncleanup resource)]
|
||||
(do
|
||||
(try
|
||||
(println "cleaning up" resource)
|
||||
(fncleanup (:resource resource))
|
||||
(catch Exception e (println "Couldn't clean up resource" resource e))))))))
|
||||
|
||||
(defn expire-outdated-resources []
|
||||
(doseq [[filename resource] @resource-cache]
|
||||
(if (not= (:last-modified resource) (last-modified filename))
|
||||
(expire-resource filename))))
|
||||
|
||||
(defn get-resource [filename fnloader & [fncleanup]]
|
||||
(if-let [resource (cached-resource filename)]
|
||||
resource
|
||||
(cache-resource filename (fnloader filename) fncleanup)))
|
||||
|
||||
(defn get-image [filename] (get-resource filename #(Image. %) #(.destroy %)))
|
||||
|
||||
(defn gc-expired-resources []
|
||||
(doseq [filename (reset-returning-old! garbage-resources #{})]
|
||||
(expire-resource-impl filename)))
|
||||
|
||||
(defn start-resource-expiry-thread []
|
||||
(.start (Thread. (fn []
|
||||
(loop []
|
||||
(expire-outdated-resources)
|
||||
(Thread/sleep 1000)
|
||||
(recur))))))
|
Loading…
Reference in a new issue