Learn R Programming

SpaDES (version 1.3.0)

cache: Cache method for simList class objects

Description

Because the simList has an environment as one of its slots, the caching mechanism of the archivist package does not work. Here, we make a slight tweak to the cache function. Specifically, we remove all elements that have an environment as part of their attributes. This is generally functions that are loaded from the modules, but also the .envir slot in the simList. Thus, only non-function objects are used as part of the digest call in the digest package (used internally in the cache function).

Usage

cache(cacheRepo = NULL, FUN, ..., notOlderThan = NULL)
"cache"(cacheRepo = NULL, FUN, ..., notOlderThan = NULL)

Arguments

cacheRepo
A repository used for storing cached objects.
FUN
A function to be called.
...
Arguments of FUN function .
notOlderThan
load an artifact from the database only if it was created after notOlderThan.

Value

Identical to cache

Details

Normally, a user will access this functionality as an argument in spades.

See Also

cache.

Examples

Run this code
## Not run: 
# mySim <- simInit(times=list(start=0.0, end=5.0),
#                  params=list(.globals=list(stackName="landscape", burnStats = "testStats")),
#                  modules=list("randomLandscapes", "fireSpread"),
#                  paths=list(modulePath=system.file("sampleModules", package="SpaDES")))
# if (require(archivist)) {
#   # Call cache function directly
#   archivist::createLocalRepo(paths(mySim)$cachePath)
#   system.time(outSim <- cache(paths(mySim)$cachePath,
#               spades, sim = copy(mySim), .plotInitialTime = NA, notOlderThan = Sys.time()))
#   system.time(outSim <- cache(paths(mySim)$cachePath,
#               spades, sim = copy(mySim), .plotInitialTime = NA))
# 
#   # This functionality can be achieved within a spades call
#   # compare caching ... run once to create cache
#   system.time(outSim <- spades(copy(mySim), cache = TRUE, notOlderThan = Sys.time(),
#                                .plotInitialTime = NA))
#   # compare... second time is fast
#   system.time(outSimCached <- spades(copy(mySim), cache = TRUE, .plotInitialTime = NA))
#   all.equal(outSim, outSimCached)
# }
# ## End(Not run)

Run the code above in your browser using DataLab