CacheVariables.cacheMethod
cache(f, path; mod = @__MODULE__)

Cache output from running f() using BSON file at path. Load if the file exists; run and save if it does not. Use mod keyword argument to specify module.

Tip: Use do...end to cache output from a block of code.

Examples

julia> cache("test.bson") do
         a = "a very time-consuming quantity to compute"
         b = "a very long simulation to run"
         return (; a = a, b = b)
       end
[ Info: Saving to test.bson
(a = "a very time-consuming quantity to compute", b = "a very long simulation to run")

julia> cache("test.bson") do
         a = "a very time-consuming quantity to compute"
         b = "a very long simulation to run"
         return (; a = a, b = b)
       end
[ Info: Loading from test.bson
(a = "a very time-consuming quantity to compute", b = "a very long simulation to run")
CacheVariables.@cacheMacro
@cache path code [overwrite]

Cache results from running code using BSON file at path. Load if the file exists; run and save if it does not. Run and save either way if overwrite is true (default is false).

Tip: Use begin...end for code to cache blocks of code.

Caveat: The variable name ans is used for storing the final output, so it is best to avoid using it as a variable name in code.

Examples

julia> @cache "test.bson" begin
         a = "a very time-consuming quantity to compute"
         b = "a very long simulation to run"
         100
       end
┌ Info: Saving to test.bson
│ a
└ b
100

julia> @cache "test.bson" begin
         a = "a very time-consuming quantity to compute"
         b = "a very long simulation to run"
         100
       end
┌ Info: Loading from test.bson
│ a
└ b
100

julia> @cache "test.bson" begin
         a = "a very time-consuming quantity to compute"
         b = "a very long simulation to run"
         100
       end true
┌ Info: Overwriting test.bson
│ a
└ b
100