cache: Cache and uncache an object

Description Usage Arguments Details See Also Examples

View source: R/cache.R

Description

Caches or uncaches and object in a cache directory

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cache(
  object,
  name = deparse(substitute(object)),
  cache = cache_path(),
  ...,
  overwrite = getOption("cache.overwrite", TRUE),
  envir = parent.frame(),
  backend = cache_backend()
)

cache_write(
  object,
  name,
  cache = cache_path(),
  ...,
  overwrite = getOption("cache.overwrite", TRUE),
  envir = parent.frame(),
  backend = cache_backend()
)

uncache(
  name,
  ...,
  envir = parent.frame(),
  overwrite = getOption("uncache.overwrite", TRUE),
  reader = cache_reader()
)

cache_read(name, cache = cache_path(), ..., reader = name_to_reader(name))

uncache_(...)

Arguments

object

object to cache

name

string; name for the object defaults to the default(substitute(object))

cache

string; path to cache directory. The default is cache_path().

...

additional arguments

overwrite

logical; whether to overwrite if it name already exists on the environment.

envir

environment .. where object to cache or to where object should be uncached. Defaults to the parent.frame()

backend

string; (name of) the backend used.

cache/cache_write and uncache/cache_read`` save and restore single objects to the cachedirectory.cache(obj)anduncache(obj)' are NSE versions that accept an unquoted name. These are mostly for interactive use.

cache_write() and cache_read() are SE versions more suitable for programatic.

cache attempts to ensure unique names of the saved object; saving an item in multiple formats is not permitted.

dispatching to cache_writer() which selects the writer for the current backend.

The cache defaults to the global option cache_path() otherwise the cache in the working directory is used. This follows the behavior in the ProjectTemplate package.

uncache_all restores all files in a cache directory.

cache also allows for timestamps. The default is to use the global option timestamp. This can either be a character vector or a function of zero arguments that returns an unary length character vector. (Only the first value is used.) Common practice is to use Sys.Date or Sys.time for creating the timestamp.

The writing of files is delegated to cache_write_x functions ...

reader

function for reading from the cache. Defaults to cache_reader()

Details

cache_write is like cache but takes a name of an object and an environment. It is mainly useful for programatic writing to the cache. cache_write will not work unless a cache direcort has been defined. See cache_path().

uncache restores an object from disk. It restores based on the name. It looks in the cache

cache_read() is a functional, no side-affect version of uncache. It reads and returns the object. Given a name, cache_read() will:

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
  ## Not run: 
     data(mtcars)
     cache(mtcars)                      # cache/mtcars.rds
     # cache( mtcars, "mycache" )           # mycache/mtcars.rds
     # cache( mtcars, "mycache", Sys.Date ) # mycache/mtcars-YYYY-MM-DD.rds

     # EXPLICIT USE OF timestamp
     # options( timestamp = Sys.Date )
     # cache( mtcars, "mycache" )          # mycache/mtcars-YYYY-MM-DD.rds

     uncache(mtcars)
     # uncache_("mtcars")

     cache_use_rds()
     cache(mtcars)
     if( exists('mtcars') ) rm(mtcars)
     uncache(mtcars)
  
## End(Not run)

decisionpatterns/cache documentation built on June 15, 2020, 9:35 p.m.