When the calculation time in Mathematica notebooks becomes long, it makes sense to save the results somewhere so that they can be easily reloaded in a new session without the need for recalculation.

Since I do this quite often, I wrote a simple function cachedCalc that automatically takes care of this. Instead of writing e.g.

integral=Integrate[Sin[x]^17, {x, 1, 5}]


cachedCalc[integral, Integrate[Sin[x]^17, {x, 1, 5}]]

and cachedCalc will take care of everything else. You can force recalculation and a max age of the cached values both for each function and also by setting global variables. Details are described in the usage message of the function (available from within Mathematica with ?cachedCalc or check the source code below).

The implementation is pretty straighforward, essentially just making use of Mathematica’s dumpSave and dumpGet functions. The only subtleties are related to variable assignments for variables whose name is given by a string, or getting the variable name as a string if a variable itself (and not its name) is given as argument for cachedCalc.