Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Created August 16, 2013 07:20
Show Gist options
  • Save martintrojer/6247939 to your computer and use it in GitHub Desktop.
Save martintrojer/6247939 to your computer and use it in GitHub Desktop.
memoize-ttl
;; 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