showCache {reproducible} | R Documentation |
Examining and modifying the cache
Description
These are convenience wrappers around DBI
package functions.
They allow the user a bit of control over what is being cached.
Usage
clearCache(
x,
userTags = character(),
after = NULL,
before = NULL,
fun = NULL,
cacheId = NULL,
ask = getOption("reproducible.ask"),
useCloud = FALSE,
cloudFolderID = getOption("reproducible.cloudFolderID", NULL),
drv = getDrv(getOption("reproducible.drv", NULL)),
conn = getOption("reproducible.conn", NULL),
verbose = getOption("reproducible.verbose"),
...
)
## S4 method for signature 'ANY'
clearCache(
x,
userTags = character(),
after = NULL,
before = NULL,
fun = NULL,
cacheId = NULL,
ask = getOption("reproducible.ask"),
useCloud = FALSE,
cloudFolderID = getOption("reproducible.cloudFolderID", NULL),
drv = getDrv(getOption("reproducible.drv", NULL)),
conn = getOption("reproducible.conn", NULL),
verbose = getOption("reproducible.verbose"),
...
)
cc(secs, ..., verbose = getOption("reproducible.verbose"))
showCache(
x,
userTags = character(),
after = NULL,
before = NULL,
fun = NULL,
cacheId = NULL,
drv = getDrv(getOption("reproducible.drv", NULL)),
conn = getOption("reproducible.conn", NULL),
verbose = getOption("reproducible.verbose"),
...
)
## S4 method for signature 'ANY'
showCache(
x,
userTags = character(),
after = NULL,
before = NULL,
fun = NULL,
cacheId = NULL,
drv = getDrv(getOption("reproducible.drv", NULL)),
conn = getOption("reproducible.conn", NULL),
verbose = getOption("reproducible.verbose"),
...
)
keepCache(
x,
userTags = character(),
after = NULL,
before = NULL,
ask = getOption("reproducible.ask"),
drv = getDrv(getOption("reproducible.drv", NULL)),
conn = getOption("reproducible.conn", NULL),
verbose = getOption("reproducible.verbose"),
...
)
## S4 method for signature 'ANY'
keepCache(
x,
userTags = character(),
after = NULL,
before = NULL,
ask = getOption("reproducible.ask"),
drv = getDrv(getOption("reproducible.drv", NULL)),
conn = getOption("reproducible.conn", NULL),
verbose = getOption("reproducible.verbose"),
...
)
Arguments
x |
A simList or a directory containing a valid Cache repository. Note:
For compatibility with |
userTags |
Character vector. If used, this will be used in place of the
|
after |
A time (POSIX, character understandable by data.table). Objects cached after this time will be shown or deleted. |
before |
A time (POSIX, character understandable by data.table). Objects cached before this time will be shown or deleted. |
fun |
An optional character vector describing the function name to extract. Only functions with this/these functions will be returned. |
cacheId |
An optional character vector describing the |
ask |
Logical. If |
useCloud |
Logical. If |
cloudFolderID |
A googledrive dribble of a folder, e.g., using |
drv |
if using a database backend, drv must be an object that inherits from DBIDriver e.g., from package RSQLite, e.g., SQLite |
conn |
an optional DBIConnection object, as returned by dbConnect(). |
verbose |
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., |
... |
Other arguments. Can be in the form of |
secs |
Currently 3 options: the number of seconds to pass to |
Details
If neither after
or before
are provided, nor userTags
,
then all objects will be removed.
If both after
and before
are specified, then all objects between
after
and before
will be deleted.
If userTags
is used, this will override after
or before
.
cc(secs)
is just a shortcut for clearCache(repo = currentRepo, after = secs)
,
i.e., to remove any cache entries touched in the last secs
seconds. Since, secs
can be missing, this is also be a shorthand for "remove most recent entry from
the cache".
clearCache
remove items from the cache based on their
userTag
ortimes
values.keepCache
remove all cached items except those based on certain
userTags
ortimes
values.showCache
display the contents of the cache.
By default the return of showCache
is sorted by cacheId
. For convenience,
a user can optionally have it unsorted (passing sorted = FALSE
),
which may be noticeably faster when
the cache is large (> 1e4
entries).
Value
Will clear all objects (or those that match userTags
, or those
between after
or before
) from the repository located at
cachePath
of the sim object, if sim
is provided, or located in
cachePath
.
Invisibly returns a data.table
of the removed items.
Note
If the cache is larger than 10MB, and clearCache is used, there will be a message and a pause, if interactive, to prevent accidentally deleting of a large cache repository.
See Also
mergeCache()
. Many more examples in Cache()
.
Examples
data.table::setDTthreads(2)
tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache")
try(clearCache(tmpDir, ask = FALSE), silent = TRUE) # just to make sure it is clear
# Basic use
ranNumsA <- Cache(rnorm, 10, 16, cachePath = tmpDir)
# All same
ranNumsB <- Cache(rnorm, 10, 16, cachePath = tmpDir) # recovers cached copy
ranNumsD <- Cache(quote(rnorm(n = 10, 16)), cachePath = tmpDir) # recovers cached copy
# Any minor change makes it different
ranNumsE <- Cache(rnorm, 10, 6, cachePath = tmpDir) # different
## Example 1: basic cache use with tags
ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
ranNumsB <- Cache(runif, 4, cachePath = tmpDir, userTags = "objectName:b")
ranNumsC <- Cache(runif, 40, cachePath = tmpDir, userTags = "objectName:b")
showCache(tmpDir, userTags = c("objectName"))
showCache(tmpDir, userTags = c("^a$")) # regular expression ... "a" exactly
# Fine control of cache elements -- pick out only the large runif object, and remove it
cache1 <- showCache(tmpDir, userTags = c("runif")) # show only cached objects made during runif
toRemove <- cache1[tagKey == "object.size"][as.numeric(tagValue) > 700]$cacheId
clearCache(tmpDir, userTags = toRemove, ask = FALSE)
cacheAfter <- showCache(tmpDir, userTags = c("runif")) # Only the small one is left
data.table::setDTthreads(2)
tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache")
try(clearCache(tmpDir, ask = FALSE), silent = TRUE) # just to make sure it is clear
Cache(rnorm, 1, cachePath = tmpDir)
thisTime <- Sys.time()
Cache(rnorm, 2, cachePath = tmpDir)
Cache(rnorm, 3, cachePath = tmpDir)
Cache(rnorm, 4, cachePath = tmpDir)
showCache(x = tmpDir) # shows all 4 entries
cc(ask = FALSE, x = tmpDir)
showCache(x = tmpDir) # most recent is gone
cc(thisTime, ask = FALSE, x = tmpDir)
showCache(x = tmpDir) # all those after thisTime gone, i.e., only 1 left
cc(ask = FALSE, x = tmpDir) # Cache is
cc(ask = FALSE, x = tmpDir) # Cache is already empty