Created
August 16, 2013 07:20
-
-
Save martintrojer/6247939 to your computer and use it in GitHub Desktop.
memoize-ttl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; http://dev.clojure.org/jira/browse/CMEMOIZE-8 | |
(defn memoize-ttl [f time-to-live] | |
(let [mem (atom {})] | |
(fn [& args] | |
(let [[_ [r t]] (find @mem args)] | |
(if (and t (<= (- (System/currentTimeMillis) t) time-to-live)) | |
r | |
(let [ret (apply f args)] | |
(swap! mem assoc args [ret (System/currentTimeMillis)]) | |
ret)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment