Created
March 7, 2018 15:51
-
-
Save mdelaurentis/3066e69ca55ee9ef932ba4753ea81a94 to your computer and use it in GitHub Desktop.
Does clojure.core.memoize cache exceptions?
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
(ns memotest │ | |
(:require [clojure.core.memoize :as memo]) │ | |
(:import [java.util Date])) │ | |
│ | |
(defn memo-test-impl [] │ | |
(when (< 0.5 (rand)) │ | |
(throw (Exception. (str (java.util.Date.))))) │ | |
(str (java.util.Date.))) │ | |
│ | |
(def memo-test (memo/ttl #'memo-test-impl :ttl/threshold 5000)) │ | |
│ | |
(def runner │ | |
(future │ | |
(while true │ | |
(Thread/sleep 1000) │ | |
(try │ | |
(println "Real value returned at" (memo-test)) │ | |
(catch Exception e │ | |
(println "Exception thrown at" (.getMessage e))))))) │ | |
│ | |
#_ (future-cancel runner) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
memo-test-impl is a memoized function that does one of two things at random:
The "runner" future periodically calls
memo-test
in a try/catch and prints the result.Here's typical output: