Skip to content

Instantly share code, notes, and snippets.

@mdelaurentis
Created March 7, 2018 15:51
Show Gist options
  • Save mdelaurentis/3066e69ca55ee9ef932ba4753ea81a94 to your computer and use it in GitHub Desktop.
Save mdelaurentis/3066e69ca55ee9ef932ba4753ea81a94 to your computer and use it in GitHub Desktop.
Does clojure.core.memoize cache exceptions?
(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)
@mdelaurentis
Copy link
Author

mdelaurentis commented Mar 7, 2018

memo-test-impl is a memoized function that does one of two things at random:

  • With 50% probability, throws an Exception with the current time as the message
  • With 50% probability, returns a string containing the current time

The "runner" future periodically calls memo-test in a try/catch and prints the result.

Here's typical output:

Real value returned at Wed Mar 07 11:11:34 EST 2018                                                                                                                                           │Real value returned at Wed Mar 07 11:11:34 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:34 EST 2018                                                                                                                                           │Exception thrown at Wed Mar 07 11:11:39 EST 2018                                                                                                                                              │
Real value returned at Wed Mar 07 11:11:40 EST 2018                                                                                                                                           │Real value returned at Wed Mar 07 11:11:40 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:40 EST 2018                                                                                                                                           │Real value returned at Wed Mar 07 11:11:40 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:44 EST 2018                                                                                                                                           │Real value returned at Wed Mar 07 11:11:44 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:44 EST 2018                                                                                                                                           │Real value returned at Wed Mar 07 11:11:44 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:44 EST 2018                                                                                                                                           │Exception thrown at Wed Mar 07 11:11:49 EST 2018                                                                                                                                              │
Exception thrown at Wed Mar 07 11:11:50 EST 2018                                                                                                                                              │Exception thrown at Wed Mar 07 11:11:51 EST 2018                                                                                                                                              │
Real value returned at Wed Mar 07 11:11:52 EST 2018                                                                                                                                           │Real value returned at Wed Mar 07 11:11:52 EST 2018                                                                                                                                           │
Exception thrown at Wed Mar 07 11:11:54 EST 2018                                                                                                                                              │Exception thrown at Wed Mar 07 11:11:55 EST 2018                                                                                                                                              │
Real value returned at Wed Mar 07 11:11:56 EST 2018                                                                                                                                           │Real value returned at Wed Mar 07 11:11:56 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:56 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:59 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:59 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:59 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:59 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:11:59 EST 2018                                                                                                                                           │
Exception thrown at Wed Mar 07 11:12:04 EST 2018                                                                                                                                              │
Real value returned at Wed Mar 07 11:12:05 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:12:05 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:12:05 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:12:05 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:12:09 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:12:09 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:12:09 EST 2018                                                                                                                                           │
user>                                                                                                                                                                                         │
Real value returned at Wed Mar 07 11:12:09 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:12:09 EST 2018                                                                                                                                           │
Real value returned at Wed Mar 07 11:12:14 EST 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment