Using the @memoize macro

Have you tried Memoize.jl?