diff --git a/README.md b/README.md index 826f6af..6e6666b 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,16 @@ julia> cache(nothing) do ``` This can be useful for conditionally saving a cache (see [Using pattern 3 on a cluster](#using-pattern-3-on-a-cluster) below). +To suppress log messages, set `verbose = false`: +```julia +julia> cache("test.bson"; verbose = false) do + a = "a very time-consuming quantity to compute" + b = "a very long simulation to run" + return (; a = a, b = b) + end +(a = "a very time-consuming quantity to compute", b = "a very long simulation to run") +``` + If you wish to access the metadata, we also provide a `cached` function that returns a `NamedTuple` containing the metadata along with the value. ```julia diff --git a/src/function.jl b/src/function.jl index 2ecd938..bcefee0 100644 --- a/src/function.jl +++ b/src/function.jl @@ -1,7 +1,7 @@ # Function form """ - cache(f, path; overwrite=false) + cache(f, path; overwrite=false, verbose=true) Cache the output of running `f()` in a cache file at `path`. The output is loaded if the file exists and is saved otherwise. @@ -21,6 +21,8 @@ e.g., to only cache a sweep when the full set is ready. If `overwrite` is set to true, existing cache files will be overwritten with the results (and metadata) from a "fresh" call to `f()`. +If `verbose` is set to false, log messages will be suppressed. + Tip: Use a `do...end` block to cache the results of a block of code. See also: [`cached`](@ref) diff --git a/test/items/function.jl b/test/items/function.jl index 6888118..757df2d 100644 --- a/test/items/function.jl +++ b/test/items/function.jl @@ -170,3 +170,38 @@ end end end end + +@testitem "cache with verbose=false" begin + mktempdir(@__DIR__; prefix = "temp_") do dirpath + @testset "$ext" for ext in ["bson", "jld2"] + path = joinpath(dirpath, "verbosetest.$ext") + + # 1. Verify no log messages when saving with verbose=false + out = @test_logs cache(path; verbose = false) do + x = collect(1:3) + y = 4 + z = "test" + return (; x = x, y = y, z = z) + end + @test out == (; x = [1, 2, 3], y = 4, z = "test") + + # 2. Verify no log messages when loading with verbose=false + out = @test_logs cache(path; verbose = false) do + x = collect(1:3) + y = 4 + z = "test" + return (; x = x, y = y, z = z) + end + @test out == (; x = [1, 2, 3], y = 4, z = "test") + + # 3. Verify no log messages when overwriting with verbose=false + out = @test_logs cache(path; overwrite = true, verbose = false) do + x = collect(1:3) + y = 4 + z = "test" + return (; x = x, y = y, z = z) + end + @test out == (; x = [1, 2, 3], y = 4, z = "test") + end + end +end