If a user manually copies a complete Cache folder (including the db file and rasters folder), there are issues that must be addressed, depending on the Cache backend used. If using DBI (e.g., RSQLite or Postgres), the db table must be renamed. Run this function after a manual copy of a cache folder. See examples for one way to do that.
Either the path of the new cachePath
where the cache was moved or copied to, or
the new DB Table Name
Optional, if there is only one table in the new
cache path.
Either the path of the previous cachePath
where the cache was moved or copied from, or
the old DB Table Name
if using a database backend, drv must be an object that inherits from DBIDriver e.g., from package RSQLite, e.g., SQLite
an optional DBIConnection object, as returned by dbConnect().
Numeric, -1 silent (where possible), 0 being very quiet,
1 showing more messaging, 2 being more messaging, etc.
Default is 1. Above 3 will output much more information about the internals of
Caching, which may help diagnose Caching challenges. Can set globally with an
option, e.g., options('reproducible.verbose' = 0) to reduce to minimal
movedCache
does not return anything; it is called for its side effects.
When the backend database for a reproducinle
cache is an SQL database, the files
on disk cannot be copied manually to a new location because they contain internal
tables. Because reproducible
gives the main table a name based on the cachePath
path, calls to Cache
will attempt to call this internally if it detects a
name mismatch.
data.table::setDTthreads(2)
tmpdir <- "tmpdir"
tmpCache <- "tmpCache"
tmpCacheDir <- normalizePath(file.path(tempdir(), tmpCache), mustWork = FALSE)
tmpdirPath <- normalizePath(file.path(tempdir(), tmpdir), mustWork = FALSE)
bb <- Cache(rnorm, 1, cachePath = tmpCacheDir)
#> Saved! Cache file: 422bae4ed2f770cc.rds; fn: rnorm
# Copy all files from tmpCache to tmpdir
froms <- normalizePath(dir(tmpCacheDir, recursive = TRUE, full.names = TRUE),
mustWork = FALSE
)
dir.create(file.path(tmpdirPath, "rasters"), recursive = TRUE, showWarnings = FALSE)
dir.create(file.path(tmpdirPath, "cacheOutputs"), recursive = TRUE, showWarnings = FALSE)
file.copy(
from = froms, overwrite = TRUE,
to = gsub(tmpCache, tmpdir, froms)
)
#> [1] TRUE TRUE
# Can use 'movedCache' to update the database table, though will generally
# happen automatically, with message indicating so
movedCache(new = tmpdirPath, old = tmpCacheDir)
bb <- Cache(rnorm, 1, cachePath = tmpdirPath) # should recover the previous call
#> Object to retrieve (fn: rnorm, 422bae4ed2f770cc.rds) ...
#> Loaded! Cached result from previous rnorm call