Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/function.jl
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions test/items/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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