Introduction to Cache

Eliot J. B. McIntire

May 29 2024

1 Reproducible workflow

As part of a reproducible workflow, caching of function calls, code chunks, and other elements of a project is a critical component. The objective of a reproducible workflow is is likely that an entire work flow from raw data to publication, decision support, report writing, presentation building etc., could be built and be reproducible anywhere, on any computer, operating system, with any starting conditions, on demand. The reproducible::Cache function is built to work with any R function.

1.1 Differences with other approaches

Cache users DBI as a backend, with key functions, dbReadTable, dbRemoveTable, dbSendQuery, dbSendStatement, dbCreateTable and dbAppendTable. These can all be accessed via Cache, showCache, clearCache, and keepCache. It is optimized for speed of transactions, using digest::digest on objects and files. The main function is superficially similar to archivist::cache, which uses digest::digest in all cases to determine whether the arguments are identical in subsequent iterations. It also but does many things that make standard caching with digest::digest don’t work reliably between systems. For these, the function .robustDigest is introduced to make caching transferable between systems. This is relevant for file paths, environments, parallel clusters, functions (which are contained within an environment), and many others (e.g., see ?.robustDigest for methods). Cache also adds important elements like automated tagging and the option to retrieve disk-cached values via stashed objects in memory using memoise::memoise. This means that running Cache 1, 2, and 3 times on the same function will get progressively faster. This can be extremely useful for web apps built with, say shiny.

1.2 Function-level caching

Any function can be cached by wrapping Cache around the function call, or by using base pipe |> or using: Cache(FUN = functionName, ...)

This will be a slight change to a function call, such as: terra::project(raster, crs = terra::crs(newRaster)) to Cache(terra::project(raster, crs = terra::crs(newRaster))) or Cache(terra::project, raster, crs = terra::crs(newRaster)) or with the pipe terra::project(raster, crs = terra::crs(newRaster)) |> Cache()

This is particularly useful for expensive operations.

library(reproducible)
library(data.table)
## 
## Attaching package: 'data.table'
## The following object is masked from 'package:terra':
## 
##     shift
tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache")
dir.create(tmpDir, recursive = TRUE)

ras <- terra::rast(terra::ext(0, 300, 0, 300), vals = 1:9e4, res = 1)
terra::crs(ras) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +datum=WGS84"

newCRS <- "+init=epsg:4326" # A longlat crs

# No Cache
system.time(suppressWarnings(map1 <- terra::project(ras, newCRS))) # Warnings due to new PROJ
##    user  system elapsed 
##   0.038   0.001   0.038
# Try with memoise for this example -- for many simple cases, memoising will not be faster
opts <- options("reproducible.useMemoise" = TRUE)
# With Cache -- a little slower the first time because saving to disk
system.time({
  suppressWarnings({
    map1 <- Cache(terra::project, ras, newCRS, cachePath = tmpDir, notOlderThan = Sys.time())
  })
})
## Saving large object (fn: terra::project, cacheId: 58442e5727a1aa3c)
##   to Cache: 9.6 Mb
##  Done!
## Saved! Cache file: 58442e5727a1aa3c.rds; fn: terra::project
##    user  system elapsed 
##   1.493   0.028   1.625
# faster the second time; improvement depends on size of object and time to run function
system.time({
  map2 <- Cache(terra::project, ras, newCRS, cachePath = tmpDir)
})
## Object to retrieve (fn: terra::project, 58442e5727a1aa3c.rds) ...
## Loaded! Memoised result from previous terra::project call
##    user  system elapsed 
##   0.116   0.008   0.133
options(opts)

all.equal(map1, map2, check.attributes = FALSE) # TRUE
## [1] "Attributes: < Component \".Cache\": Component \"newCache\": 1 element mismatch >"               
## [2] "Attributes: < Component \"ptr\": Component \"origin\": Mean relative difference: 0.0005613483 >"

1.3 Caching examples

1.3.1 Basic use

try(clearCache(tmpDir, ask = FALSE), silent = TRUE) # just to make sure it is clear

ranNumsA <- Cache(rnorm, 10, 16, cachePath = tmpDir)
## Saved! Cache file: 4fca280cda001fc9.rds; fn: rnorm
# All same
ranNumsB <- Cache(rnorm, 10, 16, cachePath = tmpDir) # recovers cached copy
## Object to retrieve (fn: rnorm, 4fca280cda001fc9.rds) ...
## Loaded! Cached result from previous rnorm call
ranNumsD1 <- Cache(quote(rnorm(n = 10, 16)), cachePath = tmpDir) # recovers cached copy
## Object to retrieve (fn: quote, 4fca280cda001fc9.rds) ...
## Loaded! Cached result from previous quote call
ranNumsD2 <- Cache(rnorm(n = 10, 16), cachePath = tmpDir) # recovers cached copy
## Object to retrieve (fn: rnorm, 4fca280cda001fc9.rds) ...
## Loaded! Cached result from previous rnorm call
# pipe
ranNumsD3 <- rnorm(n = 10, 16) |> Cache(cachePath = tmpDir) # recovers cached copy
## Object to retrieve (fn: rnorm, 4fca280cda001fc9.rds) ... 
## Loaded! Cached result from previous rnorm call
# Any minor change makes it different
ranNumsE <- Cache(rnorm, 10, 6, cachePath = tmpDir) # different
## Saved! Cache file: 5efb386c43c29ce1.rds; fn: rnorm

1.3.1.1 Example 1: Basic cache use with tags

ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: ad0ea27476c50b66.rds; fn: rnorm
ranNumsB <- Cache(runif(4), cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: deaa37372f85861b.rds; fn: runif
showCache(tmpDir, userTags = c("objectName"))
## Cache size:
##   Total (including Rasters): 504 bytes
##   Selected objects (not including Rasters): 504 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed  2024-05-29 12:22:21.55717
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.001989841 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          0.0001072884 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions                 call_block
## 15: ad0ea27476c50b66      otherFunctions                 block_exec
## 16: ad0ea27476c50b66      otherFunctions                      eng_r
## 17: ad0ea27476c50b66      otherFunctions               in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                     in_dir
## 19: ad0ea27476c50b66      otherFunctions                  timing_fn
## 20: ad0ea27476c50b66      otherFunctions                     handle
## 21: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 25: deaa37372f85861b          objectName                          b
## 26: deaa37372f85861b            function                      runif
## 27: deaa37372f85861b               class                    numeric
## 28: deaa37372f85861b         object.size                       1008
## 29: deaa37372f85861b            accessed 2024-05-29 12:22:21.575501
## 30: deaa37372f85861b             inCloud                      FALSE
## 31: deaa37372f85861b            fromDisk                      FALSE
## 32: deaa37372f85861b          resultHash                           
## 33: deaa37372f85861b   elapsedTimeDigest            0.00169158 secs
## 34: deaa37372f85861b elapsedTimeFirstRun          7.510185e-05 secs
## 35: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 36: deaa37372f85861b      otherFunctions               process_file
## 37: deaa37372f85861b      otherFunctions              process_group
## 38: deaa37372f85861b      otherFunctions                 call_block
## 39: deaa37372f85861b      otherFunctions                 block_exec
## 40: deaa37372f85861b      otherFunctions                      eng_r
## 41: deaa37372f85861b      otherFunctions               in_input_dir
## 42: deaa37372f85861b      otherFunctions                     in_dir
## 43: deaa37372f85861b      otherFunctions                  timing_fn
## 44: deaa37372f85861b      otherFunctions                     handle
## 45: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 46: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 47: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 48: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:21.558065
##  2: 2024-05-29 12:22:21.558065
##  3: 2024-05-29 12:22:21.558065
##  4: 2024-05-29 12:22:21.558065
##  5: 2024-05-29 12:22:21.558065
##  6: 2024-05-29 12:22:21.558065
##  7: 2024-05-29 12:22:21.558065
##  8: 2024-05-29 12:22:21.558065
##  9: 2024-05-29 12:22:21.558065
## 10: 2024-05-29 12:22:21.558065
## 11: 2024-05-29 12:22:21.558065
## 12: 2024-05-29 12:22:21.558065
## 13: 2024-05-29 12:22:21.558065
## 14: 2024-05-29 12:22:21.558065
## 15: 2024-05-29 12:22:21.558065
## 16: 2024-05-29 12:22:21.558065
## 17: 2024-05-29 12:22:21.558065
## 18: 2024-05-29 12:22:21.558065
## 19: 2024-05-29 12:22:21.558065
## 20: 2024-05-29 12:22:21.558065
## 21: 2024-05-29 12:22:21.558065
## 22: 2024-05-29 12:22:21.558065
## 23: 2024-05-29 12:22:21.558065
## 24: 2024-05-29 12:22:21.558065
## 25: 2024-05-29 12:22:21.576121
## 26: 2024-05-29 12:22:21.576121
## 27: 2024-05-29 12:22:21.576121
## 28: 2024-05-29 12:22:21.576121
## 29: 2024-05-29 12:22:21.576121
## 30: 2024-05-29 12:22:21.576121
## 31: 2024-05-29 12:22:21.576121
## 32: 2024-05-29 12:22:21.576121
## 33: 2024-05-29 12:22:21.576121
## 34: 2024-05-29 12:22:21.576121
## 35: 2024-05-29 12:22:21.576121
## 36: 2024-05-29 12:22:21.576121
## 37: 2024-05-29 12:22:21.576121
## 38: 2024-05-29 12:22:21.576121
## 39: 2024-05-29 12:22:21.576121
## 40: 2024-05-29 12:22:21.576121
## 41: 2024-05-29 12:22:21.576121
## 42: 2024-05-29 12:22:21.576121
## 43: 2024-05-29 12:22:21.576121
## 44: 2024-05-29 12:22:21.576121
## 45: 2024-05-29 12:22:21.576121
## 46: 2024-05-29 12:22:21.576121
## 47: 2024-05-29 12:22:21.576121
## 48: 2024-05-29 12:22:21.576121
##                    createdDate
showCache(tmpDir, userTags = c("^a$")) # regular expression ... "a" exactly
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                  tagValue
##               <char>              <char>                    <char>
##  1: ad0ea27476c50b66          objectName                         a
##  2: ad0ea27476c50b66            function                     rnorm
##  3: ad0ea27476c50b66               class                   numeric
##  4: ad0ea27476c50b66         object.size                      1008
##  5: ad0ea27476c50b66            accessed 2024-05-29 12:22:21.55717
##  6: ad0ea27476c50b66             inCloud                     FALSE
##  7: ad0ea27476c50b66            fromDisk                     FALSE
##  8: ad0ea27476c50b66          resultHash                          
##  9: ad0ea27476c50b66   elapsedTimeDigest          0.001989841 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun         0.0001072884 secs
## 11: ad0ea27476c50b66      otherFunctions          vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions              process_file
## 13: ad0ea27476c50b66      otherFunctions             process_group
## 14: ad0ea27476c50b66      otherFunctions                call_block
## 15: ad0ea27476c50b66      otherFunctions                block_exec
## 16: ad0ea27476c50b66      otherFunctions                     eng_r
## 17: ad0ea27476c50b66      otherFunctions              in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                    in_dir
## 19: ad0ea27476c50b66      otherFunctions                 timing_fn
## 20: ad0ea27476c50b66      otherFunctions                    handle
## 21: ad0ea27476c50b66           preDigest        n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest     mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest       sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest     .FUN:4f604aa46882b368
##              cacheId              tagKey                  tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:21.558065
##  2: 2024-05-29 12:22:21.558065
##  3: 2024-05-29 12:22:21.558065
##  4: 2024-05-29 12:22:21.558065
##  5: 2024-05-29 12:22:21.558065
##  6: 2024-05-29 12:22:21.558065
##  7: 2024-05-29 12:22:21.558065
##  8: 2024-05-29 12:22:21.558065
##  9: 2024-05-29 12:22:21.558065
## 10: 2024-05-29 12:22:21.558065
## 11: 2024-05-29 12:22:21.558065
## 12: 2024-05-29 12:22:21.558065
## 13: 2024-05-29 12:22:21.558065
## 14: 2024-05-29 12:22:21.558065
## 15: 2024-05-29 12:22:21.558065
## 16: 2024-05-29 12:22:21.558065
## 17: 2024-05-29 12:22:21.558065
## 18: 2024-05-29 12:22:21.558065
## 19: 2024-05-29 12:22:21.558065
## 20: 2024-05-29 12:22:21.558065
## 21: 2024-05-29 12:22:21.558065
## 22: 2024-05-29 12:22:21.558065
## 23: 2024-05-29 12:22:21.558065
## 24: 2024-05-29 12:22:21.558065
##                    createdDate
showCache(tmpDir, userTags = c("runif")) # show only cached objects made during runif call
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: deaa37372f85861b          objectName                          b
##  2: deaa37372f85861b            function                      runif
##  3: deaa37372f85861b               class                    numeric
##  4: deaa37372f85861b         object.size                       1008
##  5: deaa37372f85861b            accessed 2024-05-29 12:22:21.575501
##  6: deaa37372f85861b             inCloud                      FALSE
##  7: deaa37372f85861b            fromDisk                      FALSE
##  8: deaa37372f85861b          resultHash                           
##  9: deaa37372f85861b   elapsedTimeDigest            0.00169158 secs
## 10: deaa37372f85861b elapsedTimeFirstRun          7.510185e-05 secs
## 11: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 12: deaa37372f85861b      otherFunctions               process_file
## 13: deaa37372f85861b      otherFunctions              process_group
## 14: deaa37372f85861b      otherFunctions                 call_block
## 15: deaa37372f85861b      otherFunctions                 block_exec
## 16: deaa37372f85861b      otherFunctions                      eng_r
## 17: deaa37372f85861b      otherFunctions               in_input_dir
## 18: deaa37372f85861b      otherFunctions                     in_dir
## 19: deaa37372f85861b      otherFunctions                  timing_fn
## 20: deaa37372f85861b      otherFunctions                     handle
## 21: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 22: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 23: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 24: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:21.576121
##  2: 2024-05-29 12:22:21.576121
##  3: 2024-05-29 12:22:21.576121
##  4: 2024-05-29 12:22:21.576121
##  5: 2024-05-29 12:22:21.576121
##  6: 2024-05-29 12:22:21.576121
##  7: 2024-05-29 12:22:21.576121
##  8: 2024-05-29 12:22:21.576121
##  9: 2024-05-29 12:22:21.576121
## 10: 2024-05-29 12:22:21.576121
## 11: 2024-05-29 12:22:21.576121
## 12: 2024-05-29 12:22:21.576121
## 13: 2024-05-29 12:22:21.576121
## 14: 2024-05-29 12:22:21.576121
## 15: 2024-05-29 12:22:21.576121
## 16: 2024-05-29 12:22:21.576121
## 17: 2024-05-29 12:22:21.576121
## 18: 2024-05-29 12:22:21.576121
## 19: 2024-05-29 12:22:21.576121
## 20: 2024-05-29 12:22:21.576121
## 21: 2024-05-29 12:22:21.576121
## 22: 2024-05-29 12:22:21.576121
## 23: 2024-05-29 12:22:21.576121
## 24: 2024-05-29 12:22:21.576121
##                    createdDate
clearCache(tmpDir, userTags = c("runif"), ask = FALSE) # remove only cached objects made during runif call
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
showCache(tmpDir) # all
## Cache size:
##   Total (including Rasters): 804 bytes
##   Selected objects (not including Rasters): 804 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: 4fca280cda001fc9            function                      rnorm
##  2: 4fca280cda001fc9               class                    numeric
##  3: 4fca280cda001fc9         object.size                       1104
##  4: 4fca280cda001fc9            accessed 2024-05-29 12:22:21.415577
##  5: 4fca280cda001fc9             inCloud                      FALSE
##  6: 4fca280cda001fc9            fromDisk                      FALSE
##  7: 4fca280cda001fc9          resultHash                           
##  8: 4fca280cda001fc9   elapsedTimeDigest           0.004309177 secs
##  9: 4fca280cda001fc9 elapsedTimeFirstRun          0.0001449585 secs
## 10: 4fca280cda001fc9      otherFunctions           vweave_rmarkdown
## 11: 4fca280cda001fc9      otherFunctions               process_file
## 12: 4fca280cda001fc9      otherFunctions              process_group
## 13: 4fca280cda001fc9      otherFunctions                 call_block
## 14: 4fca280cda001fc9      otherFunctions                 block_exec
## 15: 4fca280cda001fc9      otherFunctions                      eng_r
## 16: 4fca280cda001fc9      otherFunctions               in_input_dir
## 17: 4fca280cda001fc9      otherFunctions                     in_dir
## 18: 4fca280cda001fc9      otherFunctions                  timing_fn
## 19: 4fca280cda001fc9      otherFunctions                     handle
## 20: 4fca280cda001fc9           preDigest         n:c5775c3b366fb719
## 21: 4fca280cda001fc9           preDigest      mean:15620f138033a66c
## 22: 4fca280cda001fc9           preDigest        sd:853b1797f54b229c
## 23: 4fca280cda001fc9           preDigest      .FUN:4f604aa46882b368
## 24: 4fca280cda001fc9            accessed 2024-05-29 12:22:21.436254
## 25: 4fca280cda001fc9     elapsedTimeLoad            0.00404501 secs
## 26: 4fca280cda001fc9            accessed 2024-05-29 12:22:21.461135
## 27: 4fca280cda001fc9            accessed 2024-05-29 12:22:21.481386
## 28: 4fca280cda001fc9            accessed 2024-05-29 12:22:21.499358
## 29: 5efb386c43c29ce1            function                      rnorm
## 30: 5efb386c43c29ce1               class                    numeric
## 31: 5efb386c43c29ce1         object.size                       1104
## 32: 5efb386c43c29ce1            accessed 2024-05-29 12:22:21.515569
## 33: 5efb386c43c29ce1             inCloud                      FALSE
## 34: 5efb386c43c29ce1            fromDisk                      FALSE
## 35: 5efb386c43c29ce1          resultHash                           
## 36: 5efb386c43c29ce1   elapsedTimeDigest           0.001711845 secs
## 37: 5efb386c43c29ce1 elapsedTimeFirstRun            0.00010252 secs
## 38: 5efb386c43c29ce1      otherFunctions           vweave_rmarkdown
## 39: 5efb386c43c29ce1      otherFunctions               process_file
## 40: 5efb386c43c29ce1      otherFunctions              process_group
## 41: 5efb386c43c29ce1      otherFunctions                 call_block
## 42: 5efb386c43c29ce1      otherFunctions                 block_exec
## 43: 5efb386c43c29ce1      otherFunctions                      eng_r
## 44: 5efb386c43c29ce1      otherFunctions               in_input_dir
## 45: 5efb386c43c29ce1      otherFunctions                     in_dir
## 46: 5efb386c43c29ce1      otherFunctions                  timing_fn
## 47: 5efb386c43c29ce1      otherFunctions                     handle
## 48: 5efb386c43c29ce1           preDigest         n:c5775c3b366fb719
## 49: 5efb386c43c29ce1           preDigest      mean:152602b8ff81e5bb
## 50: 5efb386c43c29ce1           preDigest        sd:853b1797f54b229c
## 51: 5efb386c43c29ce1           preDigest      .FUN:4f604aa46882b368
## 52: ad0ea27476c50b66          objectName                          a
## 53: ad0ea27476c50b66            function                      rnorm
## 54: ad0ea27476c50b66               class                    numeric
## 55: ad0ea27476c50b66         object.size                       1008
## 56: ad0ea27476c50b66            accessed  2024-05-29 12:22:21.55717
## 57: ad0ea27476c50b66             inCloud                      FALSE
## 58: ad0ea27476c50b66            fromDisk                      FALSE
## 59: ad0ea27476c50b66          resultHash                           
## 60: ad0ea27476c50b66   elapsedTimeDigest           0.001989841 secs
## 61: ad0ea27476c50b66 elapsedTimeFirstRun          0.0001072884 secs
## 62: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 63: ad0ea27476c50b66      otherFunctions               process_file
## 64: ad0ea27476c50b66      otherFunctions              process_group
## 65: ad0ea27476c50b66      otherFunctions                 call_block
## 66: ad0ea27476c50b66      otherFunctions                 block_exec
## 67: ad0ea27476c50b66      otherFunctions                      eng_r
## 68: ad0ea27476c50b66      otherFunctions               in_input_dir
## 69: ad0ea27476c50b66      otherFunctions                     in_dir
## 70: ad0ea27476c50b66      otherFunctions                  timing_fn
## 71: ad0ea27476c50b66      otherFunctions                     handle
## 72: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 73: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 74: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 75: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:21.416206
##  2: 2024-05-29 12:22:21.416206
##  3: 2024-05-29 12:22:21.416206
##  4: 2024-05-29 12:22:21.416206
##  5: 2024-05-29 12:22:21.416206
##  6: 2024-05-29 12:22:21.416206
##  7: 2024-05-29 12:22:21.416206
##  8: 2024-05-29 12:22:21.416206
##  9: 2024-05-29 12:22:21.416206
## 10: 2024-05-29 12:22:21.416206
## 11: 2024-05-29 12:22:21.416206
## 12: 2024-05-29 12:22:21.416206
## 13: 2024-05-29 12:22:21.416206
## 14: 2024-05-29 12:22:21.416206
## 15: 2024-05-29 12:22:21.416206
## 16: 2024-05-29 12:22:21.416206
## 17: 2024-05-29 12:22:21.416206
## 18: 2024-05-29 12:22:21.416206
## 19: 2024-05-29 12:22:21.416206
## 20: 2024-05-29 12:22:21.416206
## 21: 2024-05-29 12:22:21.416206
## 22: 2024-05-29 12:22:21.416206
## 23: 2024-05-29 12:22:21.416206
## 24: 2024-05-29 12:22:21.436254
## 25: 2024-05-29 12:22:21.439653
## 26: 2024-05-29 12:22:21.461135
## 27: 2024-05-29 12:22:21.481386
## 28: 2024-05-29 12:22:21.499358
## 29: 2024-05-29 12:22:21.516469
## 30: 2024-05-29 12:22:21.516469
## 31: 2024-05-29 12:22:21.516469
## 32: 2024-05-29 12:22:21.516469
## 33: 2024-05-29 12:22:21.516469
## 34: 2024-05-29 12:22:21.516469
## 35: 2024-05-29 12:22:21.516469
## 36: 2024-05-29 12:22:21.516469
## 37: 2024-05-29 12:22:21.516469
## 38: 2024-05-29 12:22:21.516469
## 39: 2024-05-29 12:22:21.516469
## 40: 2024-05-29 12:22:21.516469
## 41: 2024-05-29 12:22:21.516469
## 42: 2024-05-29 12:22:21.516469
## 43: 2024-05-29 12:22:21.516469
## 44: 2024-05-29 12:22:21.516469
## 45: 2024-05-29 12:22:21.516469
## 46: 2024-05-29 12:22:21.516469
## 47: 2024-05-29 12:22:21.516469
## 48: 2024-05-29 12:22:21.516469
## 49: 2024-05-29 12:22:21.516469
## 50: 2024-05-29 12:22:21.516469
## 51: 2024-05-29 12:22:21.516469
## 52: 2024-05-29 12:22:21.558065
## 53: 2024-05-29 12:22:21.558065
## 54: 2024-05-29 12:22:21.558065
## 55: 2024-05-29 12:22:21.558065
## 56: 2024-05-29 12:22:21.558065
## 57: 2024-05-29 12:22:21.558065
## 58: 2024-05-29 12:22:21.558065
## 59: 2024-05-29 12:22:21.558065
## 60: 2024-05-29 12:22:21.558065
## 61: 2024-05-29 12:22:21.558065
## 62: 2024-05-29 12:22:21.558065
## 63: 2024-05-29 12:22:21.558065
## 64: 2024-05-29 12:22:21.558065
## 65: 2024-05-29 12:22:21.558065
## 66: 2024-05-29 12:22:21.558065
## 67: 2024-05-29 12:22:21.558065
## 68: 2024-05-29 12:22:21.558065
## 69: 2024-05-29 12:22:21.558065
## 70: 2024-05-29 12:22:21.558065
## 71: 2024-05-29 12:22:21.558065
## 72: 2024-05-29 12:22:21.558065
## 73: 2024-05-29 12:22:21.558065
## 74: 2024-05-29 12:22:21.558065
## 75: 2024-05-29 12:22:21.558065
##                    createdDate
clearCache(tmpDir, ask = FALSE)

1.3.1.2 Example 2: using the “accessed” tag

ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: ad0ea27476c50b66.rds; fn: rnorm
ranNumsB <- Cache(runif, 4, cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: deaa37372f85861b.rds; fn: runif
# access it again, from Cache
Sys.sleep(1)
ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
## Object to retrieve (fn: rnorm, ad0ea27476c50b66.rds) ...
## Loaded! Cached result from previous rnorm call
wholeCache <- showCache(tmpDir)
## Cache size:
##   Total (including Rasters): 504 bytes
##   Selected objects (not including Rasters): 504 bytes
# keep only items accessed "recently" (i.e., only objectName:a)
onlyRecentlyAccessed <- showCache(tmpDir, userTags = max(wholeCache[tagKey == "accessed"]$tagValue))
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
# inverse join with 2 data.tables ... using: a[!b]
# i.e., return all of wholeCache that was not recently accessed
#   Note: the two different ways to access -- old way with "artifact" will be deprecated
toRemove <- unique(wholeCache[!onlyRecentlyAccessed, on = "cacheId"], by = "cacheId")$cacheId
clearCache(tmpDir, toRemove, ask = FALSE) # remove ones not recently accessed
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
showCache(tmpDir) # still has more recently accessed
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2024-05-29 12:22:21.698089
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.002384901 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun           7.43866e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions                 call_block
## 15: ad0ea27476c50b66      otherFunctions                 block_exec
## 16: ad0ea27476c50b66      otherFunctions                      eng_r
## 17: ad0ea27476c50b66      otherFunctions               in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                     in_dir
## 19: ad0ea27476c50b66      otherFunctions                  timing_fn
## 20: ad0ea27476c50b66      otherFunctions                     handle
## 21: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 25: ad0ea27476c50b66            accessed 2024-05-29 12:22:22.740265
## 26: ad0ea27476c50b66     elapsedTimeLoad            0.02228236 secs
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:21.698735
##  2: 2024-05-29 12:22:21.698735
##  3: 2024-05-29 12:22:21.698735
##  4: 2024-05-29 12:22:21.698735
##  5: 2024-05-29 12:22:21.698735
##  6: 2024-05-29 12:22:21.698735
##  7: 2024-05-29 12:22:21.698735
##  8: 2024-05-29 12:22:21.698735
##  9: 2024-05-29 12:22:21.698735
## 10: 2024-05-29 12:22:21.698735
## 11: 2024-05-29 12:22:21.698735
## 12: 2024-05-29 12:22:21.698735
## 13: 2024-05-29 12:22:21.698735
## 14: 2024-05-29 12:22:21.698735
## 15: 2024-05-29 12:22:21.698735
## 16: 2024-05-29 12:22:21.698735
## 17: 2024-05-29 12:22:21.698735
## 18: 2024-05-29 12:22:21.698735
## 19: 2024-05-29 12:22:21.698735
## 20: 2024-05-29 12:22:21.698735
## 21: 2024-05-29 12:22:21.698735
## 22: 2024-05-29 12:22:21.698735
## 23: 2024-05-29 12:22:21.698735
## 24: 2024-05-29 12:22:21.698735
## 25: 2024-05-29 12:22:22.740265
## 26: 2024-05-29 12:22:22.763137
##                    createdDate

1.3.1.3 Example 3: using keepCache

keepCache does the same as previous example, but more simply.

ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
## Object to retrieve (fn: rnorm, ad0ea27476c50b66.rds) ...
## Loaded! Cached result from previous rnorm call
ranNumsB <- Cache(runif(4), cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: deaa37372f85861b.rds; fn: runif
# keep only those cached items from the last 24 hours
oneDay <- 60 * 60 * 24
keepCache(tmpDir, after = Sys.time() - oneDay, ask = FALSE)
## Cache size:
##   Total (including Rasters): 504 bytes
##   Selected objects (not including Rasters): 504 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2024-05-29 12:22:21.698089
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.002384901 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun           7.43866e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions                 call_block
## 15: ad0ea27476c50b66      otherFunctions                 block_exec
## 16: ad0ea27476c50b66      otherFunctions                      eng_r
## 17: ad0ea27476c50b66      otherFunctions               in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                     in_dir
## 19: ad0ea27476c50b66      otherFunctions                  timing_fn
## 20: ad0ea27476c50b66      otherFunctions                     handle
## 21: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 25: ad0ea27476c50b66            accessed 2024-05-29 12:22:22.740265
## 26: ad0ea27476c50b66     elapsedTimeLoad           0.003569126 secs
## 27: ad0ea27476c50b66            accessed  2024-05-29 12:22:22.86772
## 28: deaa37372f85861b          objectName                          b
## 29: deaa37372f85861b            function                      runif
## 30: deaa37372f85861b               class                    numeric
## 31: deaa37372f85861b         object.size                       1008
## 32: deaa37372f85861b            accessed 2024-05-29 12:22:22.883291
## 33: deaa37372f85861b             inCloud                      FALSE
## 34: deaa37372f85861b            fromDisk                      FALSE
## 35: deaa37372f85861b          resultHash                           
## 36: deaa37372f85861b   elapsedTimeDigest           0.001675129 secs
## 37: deaa37372f85861b elapsedTimeFirstRun          0.0001060963 secs
## 38: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 39: deaa37372f85861b      otherFunctions               process_file
## 40: deaa37372f85861b      otherFunctions              process_group
## 41: deaa37372f85861b      otherFunctions                 call_block
## 42: deaa37372f85861b      otherFunctions                 block_exec
## 43: deaa37372f85861b      otherFunctions                      eng_r
## 44: deaa37372f85861b      otherFunctions               in_input_dir
## 45: deaa37372f85861b      otherFunctions                     in_dir
## 46: deaa37372f85861b      otherFunctions                  timing_fn
## 47: deaa37372f85861b      otherFunctions                     handle
## 48: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 49: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 50: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 51: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:21.698735
##  2: 2024-05-29 12:22:21.698735
##  3: 2024-05-29 12:22:21.698735
##  4: 2024-05-29 12:22:21.698735
##  5: 2024-05-29 12:22:21.698735
##  6: 2024-05-29 12:22:21.698735
##  7: 2024-05-29 12:22:21.698735
##  8: 2024-05-29 12:22:21.698735
##  9: 2024-05-29 12:22:21.698735
## 10: 2024-05-29 12:22:21.698735
## 11: 2024-05-29 12:22:21.698735
## 12: 2024-05-29 12:22:21.698735
## 13: 2024-05-29 12:22:21.698735
## 14: 2024-05-29 12:22:21.698735
## 15: 2024-05-29 12:22:21.698735
## 16: 2024-05-29 12:22:21.698735
## 17: 2024-05-29 12:22:21.698735
## 18: 2024-05-29 12:22:21.698735
## 19: 2024-05-29 12:22:21.698735
## 20: 2024-05-29 12:22:21.698735
## 21: 2024-05-29 12:22:21.698735
## 22: 2024-05-29 12:22:21.698735
## 23: 2024-05-29 12:22:21.698735
## 24: 2024-05-29 12:22:21.698735
## 25: 2024-05-29 12:22:22.740265
## 26: 2024-05-29 12:22:22.763137
## 27:  2024-05-29 12:22:22.86772
## 28: 2024-05-29 12:22:22.883945
## 29: 2024-05-29 12:22:22.883945
## 30: 2024-05-29 12:22:22.883945
## 31: 2024-05-29 12:22:22.883945
## 32: 2024-05-29 12:22:22.883945
## 33: 2024-05-29 12:22:22.883945
## 34: 2024-05-29 12:22:22.883945
## 35: 2024-05-29 12:22:22.883945
## 36: 2024-05-29 12:22:22.883945
## 37: 2024-05-29 12:22:22.883945
## 38: 2024-05-29 12:22:22.883945
## 39: 2024-05-29 12:22:22.883945
## 40: 2024-05-29 12:22:22.883945
## 41: 2024-05-29 12:22:22.883945
## 42: 2024-05-29 12:22:22.883945
## 43: 2024-05-29 12:22:22.883945
## 44: 2024-05-29 12:22:22.883945
## 45: 2024-05-29 12:22:22.883945
## 46: 2024-05-29 12:22:22.883945
## 47: 2024-05-29 12:22:22.883945
## 48: 2024-05-29 12:22:22.883945
## 49: 2024-05-29 12:22:22.883945
## 50: 2024-05-29 12:22:22.883945
## 51: 2024-05-29 12:22:22.883945
##                    createdDate
# Keep all Cache items created with an rnorm() call
keepCache(tmpDir, userTags = "rnorm", ask = FALSE)
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2024-05-29 12:22:21.698089
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.002384901 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun           7.43866e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions                 call_block
## 15: ad0ea27476c50b66      otherFunctions                 block_exec
## 16: ad0ea27476c50b66      otherFunctions                      eng_r
## 17: ad0ea27476c50b66      otherFunctions               in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                     in_dir
## 19: ad0ea27476c50b66      otherFunctions                  timing_fn
## 20: ad0ea27476c50b66      otherFunctions                     handle
## 21: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 25: ad0ea27476c50b66            accessed 2024-05-29 12:22:22.740265
## 26: ad0ea27476c50b66     elapsedTimeLoad           0.003569126 secs
## 27: ad0ea27476c50b66            accessed  2024-05-29 12:22:22.86772
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:21.698735
##  2: 2024-05-29 12:22:21.698735
##  3: 2024-05-29 12:22:21.698735
##  4: 2024-05-29 12:22:21.698735
##  5: 2024-05-29 12:22:21.698735
##  6: 2024-05-29 12:22:21.698735
##  7: 2024-05-29 12:22:21.698735
##  8: 2024-05-29 12:22:21.698735
##  9: 2024-05-29 12:22:21.698735
## 10: 2024-05-29 12:22:21.698735
## 11: 2024-05-29 12:22:21.698735
## 12: 2024-05-29 12:22:21.698735
## 13: 2024-05-29 12:22:21.698735
## 14: 2024-05-29 12:22:21.698735
## 15: 2024-05-29 12:22:21.698735
## 16: 2024-05-29 12:22:21.698735
## 17: 2024-05-29 12:22:21.698735
## 18: 2024-05-29 12:22:21.698735
## 19: 2024-05-29 12:22:21.698735
## 20: 2024-05-29 12:22:21.698735
## 21: 2024-05-29 12:22:21.698735
## 22: 2024-05-29 12:22:21.698735
## 23: 2024-05-29 12:22:21.698735
## 24: 2024-05-29 12:22:21.698735
## 25: 2024-05-29 12:22:22.740265
## 26: 2024-05-29 12:22:22.763137
## 27:  2024-05-29 12:22:22.86772
##                    createdDate
showCache(tmpDir)
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2024-05-29 12:22:21.698089
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.002384901 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun           7.43866e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions                 call_block
## 15: ad0ea27476c50b66      otherFunctions                 block_exec
## 16: ad0ea27476c50b66      otherFunctions                      eng_r
## 17: ad0ea27476c50b66      otherFunctions               in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                     in_dir
## 19: ad0ea27476c50b66      otherFunctions                  timing_fn
## 20: ad0ea27476c50b66      otherFunctions                     handle
## 21: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 25: ad0ea27476c50b66            accessed 2024-05-29 12:22:22.740265
## 26: ad0ea27476c50b66     elapsedTimeLoad           0.003569126 secs
## 27: ad0ea27476c50b66            accessed  2024-05-29 12:22:22.86772
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:21.698735
##  2: 2024-05-29 12:22:21.698735
##  3: 2024-05-29 12:22:21.698735
##  4: 2024-05-29 12:22:21.698735
##  5: 2024-05-29 12:22:21.698735
##  6: 2024-05-29 12:22:21.698735
##  7: 2024-05-29 12:22:21.698735
##  8: 2024-05-29 12:22:21.698735
##  9: 2024-05-29 12:22:21.698735
## 10: 2024-05-29 12:22:21.698735
## 11: 2024-05-29 12:22:21.698735
## 12: 2024-05-29 12:22:21.698735
## 13: 2024-05-29 12:22:21.698735
## 14: 2024-05-29 12:22:21.698735
## 15: 2024-05-29 12:22:21.698735
## 16: 2024-05-29 12:22:21.698735
## 17: 2024-05-29 12:22:21.698735
## 18: 2024-05-29 12:22:21.698735
## 19: 2024-05-29 12:22:21.698735
## 20: 2024-05-29 12:22:21.698735
## 21: 2024-05-29 12:22:21.698735
## 22: 2024-05-29 12:22:21.698735
## 23: 2024-05-29 12:22:21.698735
## 24: 2024-05-29 12:22:21.698735
## 25: 2024-05-29 12:22:22.740265
## 26: 2024-05-29 12:22:22.763137
## 27:  2024-05-29 12:22:22.86772
##                    createdDate
# Remove all Cache items that happened within a rnorm() call
clearCache(tmpDir, userTags = "rnorm", ask = FALSE)
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
showCache(tmpDir) ## empty
## Cache size:
##   Total (including Rasters): 0 bytes
##   Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows and 4 cols): cacheId,tagKey,tagValue,createdDate
# Also, can set a time before caching happens and remove based on this
#  --> a useful, simple way to control Cache
ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: ad0ea27476c50b66.rds; fn: rnorm
startTime <- Sys.time()
Sys.sleep(1)
ranNumsB <- Cache(rnorm, 5, cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: ccacbf62081a42b4.rds; fn: rnorm
keepCache(tmpDir, after = startTime, ask = FALSE) # keep only those newer than startTime
## Cache size:
##   Total (including Rasters): 256 bytes
##   Selected objects (not including Rasters): 256 bytes
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ccacbf62081a42b4          objectName                          b
##  2: ccacbf62081a42b4            function                      rnorm
##  3: ccacbf62081a42b4               class                    numeric
##  4: ccacbf62081a42b4         object.size                       1024
##  5: ccacbf62081a42b4            accessed 2024-05-29 12:22:24.016634
##  6: ccacbf62081a42b4             inCloud                      FALSE
##  7: ccacbf62081a42b4            fromDisk                      FALSE
##  8: ccacbf62081a42b4          resultHash                           
##  9: ccacbf62081a42b4   elapsedTimeDigest            0.00305748 secs
## 10: ccacbf62081a42b4 elapsedTimeFirstRun          0.0001018047 secs
## 11: ccacbf62081a42b4      otherFunctions           vweave_rmarkdown
## 12: ccacbf62081a42b4      otherFunctions               process_file
## 13: ccacbf62081a42b4      otherFunctions              process_group
## 14: ccacbf62081a42b4      otherFunctions                 call_block
## 15: ccacbf62081a42b4      otherFunctions                 block_exec
## 16: ccacbf62081a42b4      otherFunctions                      eng_r
## 17: ccacbf62081a42b4      otherFunctions               in_input_dir
## 18: ccacbf62081a42b4      otherFunctions                     in_dir
## 19: ccacbf62081a42b4      otherFunctions                  timing_fn
## 20: ccacbf62081a42b4      otherFunctions                     handle
## 21: ccacbf62081a42b4           preDigest         n:a4f076b3db622faf
## 22: ccacbf62081a42b4           preDigest      mean:c40c00762a0dac94
## 23: ccacbf62081a42b4           preDigest        sd:853b1797f54b229c
## 24: ccacbf62081a42b4           preDigest      .FUN:4f604aa46882b368
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:24.017504
##  2: 2024-05-29 12:22:24.017504
##  3: 2024-05-29 12:22:24.017504
##  4: 2024-05-29 12:22:24.017504
##  5: 2024-05-29 12:22:24.017504
##  6: 2024-05-29 12:22:24.017504
##  7: 2024-05-29 12:22:24.017504
##  8: 2024-05-29 12:22:24.017504
##  9: 2024-05-29 12:22:24.017504
## 10: 2024-05-29 12:22:24.017504
## 11: 2024-05-29 12:22:24.017504
## 12: 2024-05-29 12:22:24.017504
## 13: 2024-05-29 12:22:24.017504
## 14: 2024-05-29 12:22:24.017504
## 15: 2024-05-29 12:22:24.017504
## 16: 2024-05-29 12:22:24.017504
## 17: 2024-05-29 12:22:24.017504
## 18: 2024-05-29 12:22:24.017504
## 19: 2024-05-29 12:22:24.017504
## 20: 2024-05-29 12:22:24.017504
## 21: 2024-05-29 12:22:24.017504
## 22: 2024-05-29 12:22:24.017504
## 23: 2024-05-29 12:22:24.017504
## 24: 2024-05-29 12:22:24.017504
##                    createdDate
clearCache(tmpDir, ask = FALSE)

1.3.1.4 Example 4: searching for multiple objects in the cache

# default userTags is "and" matching; for "or" matching use |
ranNumsA <- Cache(runif, 4, cachePath = tmpDir, userTags = "objectName:a")
## Saved! Cache file: deaa37372f85861b.rds; fn: runif
ranNumsB <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:b")
## Saved! Cache file: ad0ea27476c50b66.rds; fn: rnorm
# show all objects (runif and rnorm in this case)
showCache(tmpDir)
## Cache size:
##   Total (including Rasters): 504 bytes
##   Selected objects (not including Rasters): 504 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ad0ea27476c50b66          objectName                          b
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2024-05-29 12:22:24.145506
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.003247976 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          8.749962e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions                 call_block
## 15: ad0ea27476c50b66      otherFunctions                 block_exec
## 16: ad0ea27476c50b66      otherFunctions                      eng_r
## 17: ad0ea27476c50b66      otherFunctions               in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                     in_dir
## 19: ad0ea27476c50b66      otherFunctions                  timing_fn
## 20: ad0ea27476c50b66      otherFunctions                     handle
## 21: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 25: deaa37372f85861b          objectName                          a
## 26: deaa37372f85861b            function                      runif
## 27: deaa37372f85861b               class                    numeric
## 28: deaa37372f85861b         object.size                       1008
## 29: deaa37372f85861b            accessed 2024-05-29 12:22:24.123631
## 30: deaa37372f85861b             inCloud                      FALSE
## 31: deaa37372f85861b            fromDisk                      FALSE
## 32: deaa37372f85861b          resultHash                           
## 33: deaa37372f85861b   elapsedTimeDigest           0.002550125 secs
## 34: deaa37372f85861b elapsedTimeFirstRun          7.629395e-05 secs
## 35: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 36: deaa37372f85861b      otherFunctions               process_file
## 37: deaa37372f85861b      otherFunctions              process_group
## 38: deaa37372f85861b      otherFunctions                 call_block
## 39: deaa37372f85861b      otherFunctions                 block_exec
## 40: deaa37372f85861b      otherFunctions                      eng_r
## 41: deaa37372f85861b      otherFunctions               in_input_dir
## 42: deaa37372f85861b      otherFunctions                     in_dir
## 43: deaa37372f85861b      otherFunctions                  timing_fn
## 44: deaa37372f85861b      otherFunctions                     handle
## 45: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 46: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 47: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 48: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:24.146147
##  2: 2024-05-29 12:22:24.146147
##  3: 2024-05-29 12:22:24.146147
##  4: 2024-05-29 12:22:24.146147
##  5: 2024-05-29 12:22:24.146147
##  6: 2024-05-29 12:22:24.146147
##  7: 2024-05-29 12:22:24.146147
##  8: 2024-05-29 12:22:24.146147
##  9: 2024-05-29 12:22:24.146147
## 10: 2024-05-29 12:22:24.146147
## 11: 2024-05-29 12:22:24.146147
## 12: 2024-05-29 12:22:24.146147
## 13: 2024-05-29 12:22:24.146147
## 14: 2024-05-29 12:22:24.146147
## 15: 2024-05-29 12:22:24.146147
## 16: 2024-05-29 12:22:24.146147
## 17: 2024-05-29 12:22:24.146147
## 18: 2024-05-29 12:22:24.146147
## 19: 2024-05-29 12:22:24.146147
## 20: 2024-05-29 12:22:24.146147
## 21: 2024-05-29 12:22:24.146147
## 22: 2024-05-29 12:22:24.146147
## 23: 2024-05-29 12:22:24.146147
## 24: 2024-05-29 12:22:24.146147
## 25: 2024-05-29 12:22:24.124261
## 26: 2024-05-29 12:22:24.124261
## 27: 2024-05-29 12:22:24.124261
## 28: 2024-05-29 12:22:24.124261
## 29: 2024-05-29 12:22:24.124261
## 30: 2024-05-29 12:22:24.124261
## 31: 2024-05-29 12:22:24.124261
## 32: 2024-05-29 12:22:24.124261
## 33: 2024-05-29 12:22:24.124261
## 34: 2024-05-29 12:22:24.124261
## 35: 2024-05-29 12:22:24.124261
## 36: 2024-05-29 12:22:24.124261
## 37: 2024-05-29 12:22:24.124261
## 38: 2024-05-29 12:22:24.124261
## 39: 2024-05-29 12:22:24.124261
## 40: 2024-05-29 12:22:24.124261
## 41: 2024-05-29 12:22:24.124261
## 42: 2024-05-29 12:22:24.124261
## 43: 2024-05-29 12:22:24.124261
## 44: 2024-05-29 12:22:24.124261
## 45: 2024-05-29 12:22:24.124261
## 46: 2024-05-29 12:22:24.124261
## 47: 2024-05-29 12:22:24.124261
## 48: 2024-05-29 12:22:24.124261
##                    createdDate
# show objects that are both runif and rnorm
# (i.e., none in this case, because objecs are either or, not both)
showCache(tmpDir, userTags = c("runif", "rnorm")) ## empty
## Cache size:
##   Total (including Rasters): 0 bytes
##   Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows and 4 cols): cacheId,tagKey,tagValue,createdDate
# show objects that are either runif or rnorm ("or" search)
showCache(tmpDir, userTags = "runif|rnorm")
## Cache size:
##   Total (including Rasters): 504 bytes
##   Selected objects (not including Rasters): 504 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ad0ea27476c50b66          objectName                          b
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2024-05-29 12:22:24.145506
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.003247976 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          8.749962e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions                 call_block
## 15: ad0ea27476c50b66      otherFunctions                 block_exec
## 16: ad0ea27476c50b66      otherFunctions                      eng_r
## 17: ad0ea27476c50b66      otherFunctions               in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                     in_dir
## 19: ad0ea27476c50b66      otherFunctions                  timing_fn
## 20: ad0ea27476c50b66      otherFunctions                     handle
## 21: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 25: deaa37372f85861b          objectName                          a
## 26: deaa37372f85861b            function                      runif
## 27: deaa37372f85861b               class                    numeric
## 28: deaa37372f85861b         object.size                       1008
## 29: deaa37372f85861b            accessed 2024-05-29 12:22:24.123631
## 30: deaa37372f85861b             inCloud                      FALSE
## 31: deaa37372f85861b            fromDisk                      FALSE
## 32: deaa37372f85861b          resultHash                           
## 33: deaa37372f85861b   elapsedTimeDigest           0.002550125 secs
## 34: deaa37372f85861b elapsedTimeFirstRun          7.629395e-05 secs
## 35: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 36: deaa37372f85861b      otherFunctions               process_file
## 37: deaa37372f85861b      otherFunctions              process_group
## 38: deaa37372f85861b      otherFunctions                 call_block
## 39: deaa37372f85861b      otherFunctions                 block_exec
## 40: deaa37372f85861b      otherFunctions                      eng_r
## 41: deaa37372f85861b      otherFunctions               in_input_dir
## 42: deaa37372f85861b      otherFunctions                     in_dir
## 43: deaa37372f85861b      otherFunctions                  timing_fn
## 44: deaa37372f85861b      otherFunctions                     handle
## 45: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 46: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 47: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 48: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:24.146147
##  2: 2024-05-29 12:22:24.146147
##  3: 2024-05-29 12:22:24.146147
##  4: 2024-05-29 12:22:24.146147
##  5: 2024-05-29 12:22:24.146147
##  6: 2024-05-29 12:22:24.146147
##  7: 2024-05-29 12:22:24.146147
##  8: 2024-05-29 12:22:24.146147
##  9: 2024-05-29 12:22:24.146147
## 10: 2024-05-29 12:22:24.146147
## 11: 2024-05-29 12:22:24.146147
## 12: 2024-05-29 12:22:24.146147
## 13: 2024-05-29 12:22:24.146147
## 14: 2024-05-29 12:22:24.146147
## 15: 2024-05-29 12:22:24.146147
## 16: 2024-05-29 12:22:24.146147
## 17: 2024-05-29 12:22:24.146147
## 18: 2024-05-29 12:22:24.146147
## 19: 2024-05-29 12:22:24.146147
## 20: 2024-05-29 12:22:24.146147
## 21: 2024-05-29 12:22:24.146147
## 22: 2024-05-29 12:22:24.146147
## 23: 2024-05-29 12:22:24.146147
## 24: 2024-05-29 12:22:24.146147
## 25: 2024-05-29 12:22:24.124261
## 26: 2024-05-29 12:22:24.124261
## 27: 2024-05-29 12:22:24.124261
## 28: 2024-05-29 12:22:24.124261
## 29: 2024-05-29 12:22:24.124261
## 30: 2024-05-29 12:22:24.124261
## 31: 2024-05-29 12:22:24.124261
## 32: 2024-05-29 12:22:24.124261
## 33: 2024-05-29 12:22:24.124261
## 34: 2024-05-29 12:22:24.124261
## 35: 2024-05-29 12:22:24.124261
## 36: 2024-05-29 12:22:24.124261
## 37: 2024-05-29 12:22:24.124261
## 38: 2024-05-29 12:22:24.124261
## 39: 2024-05-29 12:22:24.124261
## 40: 2024-05-29 12:22:24.124261
## 41: 2024-05-29 12:22:24.124261
## 42: 2024-05-29 12:22:24.124261
## 43: 2024-05-29 12:22:24.124261
## 44: 2024-05-29 12:22:24.124261
## 45: 2024-05-29 12:22:24.124261
## 46: 2024-05-29 12:22:24.124261
## 47: 2024-05-29 12:22:24.124261
## 48: 2024-05-29 12:22:24.124261
##                    createdDate
# keep only objects that are either runif or rnorm ("or" search)
keepCache(tmpDir, userTags = "runif|rnorm", ask = FALSE)
## Cache size:
##   Total (including Rasters): 504 bytes
##   Selected objects (not including Rasters): 504 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: ad0ea27476c50b66          objectName                          b
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2024-05-29 12:22:24.145506
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.003247976 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          8.749962e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions                 call_block
## 15: ad0ea27476c50b66      otherFunctions                 block_exec
## 16: ad0ea27476c50b66      otherFunctions                      eng_r
## 17: ad0ea27476c50b66      otherFunctions               in_input_dir
## 18: ad0ea27476c50b66      otherFunctions                     in_dir
## 19: ad0ea27476c50b66      otherFunctions                  timing_fn
## 20: ad0ea27476c50b66      otherFunctions                     handle
## 21: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 22: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 23: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 24: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 25: deaa37372f85861b          objectName                          a
## 26: deaa37372f85861b            function                      runif
## 27: deaa37372f85861b               class                    numeric
## 28: deaa37372f85861b         object.size                       1008
## 29: deaa37372f85861b            accessed 2024-05-29 12:22:24.123631
## 30: deaa37372f85861b             inCloud                      FALSE
## 31: deaa37372f85861b            fromDisk                      FALSE
## 32: deaa37372f85861b          resultHash                           
## 33: deaa37372f85861b   elapsedTimeDigest           0.002550125 secs
## 34: deaa37372f85861b elapsedTimeFirstRun          7.629395e-05 secs
## 35: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 36: deaa37372f85861b      otherFunctions               process_file
## 37: deaa37372f85861b      otherFunctions              process_group
## 38: deaa37372f85861b      otherFunctions                 call_block
## 39: deaa37372f85861b      otherFunctions                 block_exec
## 40: deaa37372f85861b      otherFunctions                      eng_r
## 41: deaa37372f85861b      otherFunctions               in_input_dir
## 42: deaa37372f85861b      otherFunctions                     in_dir
## 43: deaa37372f85861b      otherFunctions                  timing_fn
## 44: deaa37372f85861b      otherFunctions                     handle
## 45: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 46: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 47: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 48: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:24.146147
##  2: 2024-05-29 12:22:24.146147
##  3: 2024-05-29 12:22:24.146147
##  4: 2024-05-29 12:22:24.146147
##  5: 2024-05-29 12:22:24.146147
##  6: 2024-05-29 12:22:24.146147
##  7: 2024-05-29 12:22:24.146147
##  8: 2024-05-29 12:22:24.146147
##  9: 2024-05-29 12:22:24.146147
## 10: 2024-05-29 12:22:24.146147
## 11: 2024-05-29 12:22:24.146147
## 12: 2024-05-29 12:22:24.146147
## 13: 2024-05-29 12:22:24.146147
## 14: 2024-05-29 12:22:24.146147
## 15: 2024-05-29 12:22:24.146147
## 16: 2024-05-29 12:22:24.146147
## 17: 2024-05-29 12:22:24.146147
## 18: 2024-05-29 12:22:24.146147
## 19: 2024-05-29 12:22:24.146147
## 20: 2024-05-29 12:22:24.146147
## 21: 2024-05-29 12:22:24.146147
## 22: 2024-05-29 12:22:24.146147
## 23: 2024-05-29 12:22:24.146147
## 24: 2024-05-29 12:22:24.146147
## 25: 2024-05-29 12:22:24.124261
## 26: 2024-05-29 12:22:24.124261
## 27: 2024-05-29 12:22:24.124261
## 28: 2024-05-29 12:22:24.124261
## 29: 2024-05-29 12:22:24.124261
## 30: 2024-05-29 12:22:24.124261
## 31: 2024-05-29 12:22:24.124261
## 32: 2024-05-29 12:22:24.124261
## 33: 2024-05-29 12:22:24.124261
## 34: 2024-05-29 12:22:24.124261
## 35: 2024-05-29 12:22:24.124261
## 36: 2024-05-29 12:22:24.124261
## 37: 2024-05-29 12:22:24.124261
## 38: 2024-05-29 12:22:24.124261
## 39: 2024-05-29 12:22:24.124261
## 40: 2024-05-29 12:22:24.124261
## 41: 2024-05-29 12:22:24.124261
## 42: 2024-05-29 12:22:24.124261
## 43: 2024-05-29 12:22:24.124261
## 44: 2024-05-29 12:22:24.124261
## 45: 2024-05-29 12:22:24.124261
## 46: 2024-05-29 12:22:24.124261
## 47: 2024-05-29 12:22:24.124261
## 48: 2024-05-29 12:22:24.124261
##                    createdDate
clearCache(tmpDir, ask = FALSE)

1.3.1.5 Example 5: using caching to speed up rerunning expensive computations

ras <- terra::rast(terra::ext(0, 5, 0, 5),
  res = 1,
  vals = sample(1:5, replace = TRUE, size = 25),
  crs = "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
)

rasCRS <- terra::crs(ras)
# A slow operation, like GIS operation
notCached <- suppressWarnings(
  # project raster generates warnings when run non-interactively
  terra::project(ras, rasCRS, res = 5)
)

cached <- suppressWarnings(
  # project raster generates warnings when run non-interactively
  # using quote works also
  Cache(terra::project, ras, rasCRS, res = 5, cachePath = tmpDir)
)
## Saved! Cache file: 660b6470fe0d7d27.rds; fn: terra::project
# second time is much faster
reRun <- suppressWarnings(
  # project raster generates warnings when run non-interactively
  Cache(terra::project, ras, rasCRS, res = 5, cachePath = tmpDir)
)
## Object to retrieve (fn: terra::project, 660b6470fe0d7d27.rds) ...
## Loaded! Cached result from previous terra::project call
# recovered cached version is same as non-cached version
all.equal(notCached, reRun, check.attributes = FALSE) ## TRUE
## [1] "Attributes: < Names: 2 string mismatches >"                                                    
## [2] "Attributes: < Length mismatch: comparison on first 2 components >"                             
## [3] "Attributes: < Component 1: Modes: character, list >"                                           
## [4] "Attributes: < Component 1: names for current but not for target >"                             
## [5] "Attributes: < Component 1: Attributes: < names for target but not for current > >"             
## [6] "Attributes: < Component 1: Attributes: < Length mismatch: comparison on first 0 components > >"
## [7] "Attributes: < Component 1: target is character, current is list >"                             
## [8] "Attributes: < Component 2: 'current' is not an envRefClass >"

1.3.2 Nested Caching

Nested caching, which is when Caching of a function occurs inside an outer function, which is itself cached. This is a critical element to working within a reproducible work flow. It is not enough during development to cache flat code chunks, as there will be many levels of “slow” functions. Ideally, at all points in a development cycle, it should be possible to get to any line of code starting from the very initial steps, running through everything up to that point, in less than a few seconds. If the workflow can be kept very fast like this, then there is a guarantee that it will work at any point.

##########################
## Nested Caching
# Make 2 functions
inner <- function(mean) {
  d <- 1
  Cache(rnorm, n = 3, mean = mean)
}
outer <- function(n) {
  Cache(inner, 0.1, cachePath = tmpdir2)
}

# make 2 different cache paths
tmpdir1 <- file.path(tempdir(), "first")
tmpdir2 <- file.path(tempdir(), "second")

# Run the Cache ... notOlderThan propagates to all 3 Cache calls,
#   but cachePath is tmpdir1 in top level Cache and all nested
#   Cache calls, unless individually overridden ... here inner
#   uses tmpdir2 repository
Cache(outer, n = 2, cachePath = tmpdir1, notOlderThan = Sys.time())
##   No cachePath supplied and getOption('reproducible.cachePath') is
##     inside a temporary directory;
##     this will not persist across R
##     sessions.
##   Saved! Cache file: efa1ccee79a31d4c.rds; fn: rnorm
##  Saved! Cache file: 33ceb4fb525fd08f.rds; fn: inner
## Saved! Cache file: dffc8e3bf8c23b6e.rds; fn: outer
## [1] -0.17713042 -0.62668385  0.09118464
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"tags")
## [1] "cacheId:dffc8e3bf8c23b6e"
## attr(,"call")
## [1] ""
showCache(tmpdir1) # 2 function calls
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: dffc8e3bf8c23b6e            function                      outer
##  2: dffc8e3bf8c23b6e               class                    numeric
##  3: dffc8e3bf8c23b6e         object.size                       1008
##  4: dffc8e3bf8c23b6e            accessed 2024-05-29 12:22:26.532689
##  5: dffc8e3bf8c23b6e             inCloud                      FALSE
##  6: dffc8e3bf8c23b6e            fromDisk                      FALSE
##  7: dffc8e3bf8c23b6e          resultHash                           
##  8: dffc8e3bf8c23b6e   elapsedTimeDigest           0.001784563 secs
##  9: dffc8e3bf8c23b6e elapsedTimeFirstRun            0.05833006 secs
## 10: dffc8e3bf8c23b6e      otherFunctions           vweave_rmarkdown
## 11: dffc8e3bf8c23b6e      otherFunctions               process_file
## 12: dffc8e3bf8c23b6e      otherFunctions              process_group
## 13: dffc8e3bf8c23b6e      otherFunctions                 call_block
## 14: dffc8e3bf8c23b6e      otherFunctions                 block_exec
## 15: dffc8e3bf8c23b6e      otherFunctions                      eng_r
## 16: dffc8e3bf8c23b6e      otherFunctions               in_input_dir
## 17: dffc8e3bf8c23b6e      otherFunctions                     in_dir
## 18: dffc8e3bf8c23b6e      otherFunctions                  timing_fn
## 19: dffc8e3bf8c23b6e      otherFunctions                     handle
## 20: dffc8e3bf8c23b6e           preDigest         n:82dc709f2b91918a
## 21: dffc8e3bf8c23b6e           preDigest      .FUN:b89dd186387954a0
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:26.533335
##  2: 2024-05-29 12:22:26.533335
##  3: 2024-05-29 12:22:26.533335
##  4: 2024-05-29 12:22:26.533335
##  5: 2024-05-29 12:22:26.533335
##  6: 2024-05-29 12:22:26.533335
##  7: 2024-05-29 12:22:26.533335
##  8: 2024-05-29 12:22:26.533335
##  9: 2024-05-29 12:22:26.533335
## 10: 2024-05-29 12:22:26.533335
## 11: 2024-05-29 12:22:26.533335
## 12: 2024-05-29 12:22:26.533335
## 13: 2024-05-29 12:22:26.533335
## 14: 2024-05-29 12:22:26.533335
## 15: 2024-05-29 12:22:26.533335
## 16: 2024-05-29 12:22:26.533335
## 17: 2024-05-29 12:22:26.533335
## 18: 2024-05-29 12:22:26.533335
## 19: 2024-05-29 12:22:26.533335
## 20: 2024-05-29 12:22:26.533335
## 21: 2024-05-29 12:22:26.533335
##                    createdDate
showCache(tmpdir2) # 1 function call
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: 33ceb4fb525fd08f            function                      inner
##  2: 33ceb4fb525fd08f               class                    numeric
##  3: 33ceb4fb525fd08f         object.size                       1008
##  4: 33ceb4fb525fd08f            accessed 2024-05-29 12:22:26.523633
##  5: 33ceb4fb525fd08f             inCloud                      FALSE
##  6: 33ceb4fb525fd08f            fromDisk                      FALSE
##  7: 33ceb4fb525fd08f          resultHash                           
##  8: 33ceb4fb525fd08f   elapsedTimeDigest            0.00178647 secs
##  9: 33ceb4fb525fd08f elapsedTimeFirstRun            0.03139472 secs
## 10: 33ceb4fb525fd08f      otherFunctions           vweave_rmarkdown
## 11: 33ceb4fb525fd08f      otherFunctions               process_file
## 12: 33ceb4fb525fd08f      otherFunctions              process_group
## 13: 33ceb4fb525fd08f      otherFunctions                 call_block
## 14: 33ceb4fb525fd08f      otherFunctions                 block_exec
## 15: 33ceb4fb525fd08f      otherFunctions                      eng_r
## 16: 33ceb4fb525fd08f      otherFunctions               in_input_dir
## 17: 33ceb4fb525fd08f      otherFunctions                     in_dir
## 18: 33ceb4fb525fd08f      otherFunctions                  timing_fn
## 19: 33ceb4fb525fd08f      otherFunctions                     handle
## 20: 33ceb4fb525fd08f      otherFunctions                        out
## 21: 33ceb4fb525fd08f           preDigest      mean:22413394efd9f6a3
## 22: 33ceb4fb525fd08f           preDigest      .FUN:87e2c30917a34d25
##              cacheId              tagKey                   tagValue
##                    createdDate
##                         <char>
##  1: 2024-05-29 12:22:26.524235
##  2: 2024-05-29 12:22:26.524235
##  3: 2024-05-29 12:22:26.524235
##  4: 2024-05-29 12:22:26.524235
##  5: 2024-05-29 12:22:26.524235
##  6: 2024-05-29 12:22:26.524235
##  7: 2024-05-29 12:22:26.524235
##  8: 2024-05-29 12:22:26.524235
##  9: 2024-05-29 12:22:26.524235
## 10: 2024-05-29 12:22:26.524235
## 11: 2024-05-29 12:22:26.524235
## 12: 2024-05-29 12:22:26.524235
## 13: 2024-05-29 12:22:26.524235
## 14: 2024-05-29 12:22:26.524235
## 15: 2024-05-29 12:22:26.524235
## 16: 2024-05-29 12:22:26.524235
## 17: 2024-05-29 12:22:26.524235
## 18: 2024-05-29 12:22:26.524235
## 19: 2024-05-29 12:22:26.524235
## 20: 2024-05-29 12:22:26.524235
## 21: 2024-05-29 12:22:26.524235
## 22: 2024-05-29 12:22:26.524235
##                    createdDate
# userTags get appended
# all items have the outer tag propagate, plus inner ones only have inner ones
clearCache(tmpdir1, ask = FALSE)
outerTag <- "outerTag"
innerTag <- "innerTag"
inner <- function(mean) {
  d <- 1
  Cache(rnorm, n = 3, mean = mean, notOlderThan = Sys.time() - 1e5, userTags = innerTag)
}
outer <- function(n) {
  Cache(inner, 0.1)
}
aa <- Cache(outer, n = 2, cachePath = tmpdir1, userTags = outerTag)
##  No cachePath supplied and getOption('reproducible.cachePath') is
##    inside a temporary directory;
##    this will not persist across R
##    sessions.
##   No cachePath supplied and getOption('reproducible.cachePath') is
##     inside a temporary directory;
##     this will not persist across R
##     sessions.
##   Object to retrieve (fn: rnorm, efa1ccee79a31d4c.rds) ...
##   Loaded! Cached result from previous rnorm call
##  Saved! Cache file: b06af03d5a73dc7d.rds; fn: inner
## Saved! Cache file: 88a34e1d033329e5.rds; fn: outer
showCache(tmpdir1) # rnorm function has outerTag and innerTag, inner and outer only have outerTag
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##               <char>              <char>                     <char>
##  1: 88a34e1d033329e5            outerTag                   outerTag
##  2: 88a34e1d033329e5            function                      outer
##  3: 88a34e1d033329e5               class                    numeric
##  4: 88a34e1d033329e5         object.size                       1008
##  5: 88a34e1d033329e5            accessed 2024-05-29 12:22:26.641759
##  6: 88a34e1d033329e5             inCloud                      FALSE
##  7: 88a34e1d033329e5            fromDisk                      FALSE
##  8: 88a34e1d033329e5          resultHash                           
##  9: 88a34e1d033329e5   elapsedTimeDigest           0.001648426 secs
## 10: 88a34e1d033329e5 elapsedTimeFirstRun            0.03729272 secs
## 11: 88a34e1d033329e5      otherFunctions           vweave_rmarkdown
## 12: 88a34e1d033329e5      otherFunctions               process_file
## 13: 88a34e1d033329e5      otherFunctions              process_group
## 14: 88a34e1d033329e5      otherFunctions                 call_block
## 15: 88a34e1d033329e5      otherFunctions                 block_exec
## 16: 88a34e1d033329e5      otherFunctions                      eng_r
## 17: 88a34e1d033329e5      otherFunctions               in_input_dir
## 18: 88a34e1d033329e5      otherFunctions                     in_dir
## 19: 88a34e1d033329e5      otherFunctions                  timing_fn
## 20: 88a34e1d033329e5      otherFunctions                     handle
## 21: 88a34e1d033329e5           preDigest         n:82dc709f2b91918a
## 22: 88a34e1d033329e5           preDigest      .FUN:5f06fb5fbffe9e3b
##              cacheId              tagKey                   tagValue
##                   createdDate
##                        <char>
##  1: 2024-05-29 12:22:26.64241
##  2: 2024-05-29 12:22:26.64241
##  3: 2024-05-29 12:22:26.64241
##  4: 2024-05-29 12:22:26.64241
##  5: 2024-05-29 12:22:26.64241
##  6: 2024-05-29 12:22:26.64241
##  7: 2024-05-29 12:22:26.64241
##  8: 2024-05-29 12:22:26.64241
##  9: 2024-05-29 12:22:26.64241
## 10: 2024-05-29 12:22:26.64241
## 11: 2024-05-29 12:22:26.64241
## 12: 2024-05-29 12:22:26.64241
## 13: 2024-05-29 12:22:26.64241
## 14: 2024-05-29 12:22:26.64241
## 15: 2024-05-29 12:22:26.64241
## 16: 2024-05-29 12:22:26.64241
## 17: 2024-05-29 12:22:26.64241
## 18: 2024-05-29 12:22:26.64241
## 19: 2024-05-29 12:22:26.64241
## 20: 2024-05-29 12:22:26.64241
## 21: 2024-05-29 12:22:26.64241
## 22: 2024-05-29 12:22:26.64241
##                   createdDate

1.3.3 cacheId

Sometimes, it is not absolutely desirable to maintain the work flow intact because changes that are irrelevant to the analysis, such as changing messages sent to a user, may be changed, without a desire to rerun functions. The cacheId argument is for this. Once a piece of code is run, then the cacheId can be manually extracted (it is reported at the end of a Cache call) and manually placed in the code, passed in as, say, cacheId = "ad184ce64541972b50afd8e7b75f821b".

### cacheId
set.seed(1)
Cache(rnorm, 1, cachePath = tmpdir1)
## Saved! Cache file: 422bae4ed2f770cc.rds; fn: rnorm
## [1] 1.511781
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"tags")
## [1] "cacheId:422bae4ed2f770cc"
## attr(,"call")
## [1] ""
# manually look at output attribute which shows cacheId: 7072c305d8c69df0
Cache(rnorm, 1, cachePath = tmpdir1, cacheId = "422bae4ed2f770cc") # same value
## cacheId is same as calculated hash
## Object to retrieve (fn: rnorm, 422bae4ed2f770cc.rds) ...
## Loaded! Cached result from previous rnorm call
## [1] 1.511781
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] FALSE
## 
## attr(,"tags")
## [1] "cacheId:422bae4ed2f770cc"
## attr(,"call")
## [1] ""
# override even with different inputs:
Cache(rnorm, 2, cachePath = tmpdir1, cacheId = "422bae4ed2f770cc")
## cacheId is not same as calculated hash. Manually searching for
##   cacheId:422bae4ed2f770cc
## Saved! Cache file: 422bae4ed2f770cc.rds; fn: rnorm
## [1] -1.3770596 -0.4149946
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"tags")
## [1] "cacheId:422bae4ed2f770cc"
## attr(,"call")
## [1] ""

1.4 Working with the Cache manually

Since the cache is simply a DBI data table (of an SQLite database by default). In addition, there are several helpers in the reproducible package, including showCache, keepCache and clearCache that may be useful. Also, one can access cached items manually (rather than simply rerunning the same Cache function again).

# As of reproducible version 1.0, there is a new backend directly using DBI
mapHash <- unique(showCache(tmpDir, userTags = "project")$cacheId)
## Cache size:
##   Total (including Rasters): 2.2 Mb
##   Selected objects (not including Rasters): 2.2 Mb
map <- loadFromCache(mapHash[1], cachePath = tmpDir)
## Loaded! Cached result from previous  call
terra::plot(map)

## cleanup
unlink(dirname(tmpDir), recursive = TRUE)

2 Alternative database backends

By default, caching relies on a sqlite database for it’s backend. While this works in many situations, there are some important limitations of using sqlite for caching, including 1) speed; 2) concurrent transactions; 3) sharing database across machines or projects. Fortunately, Cache makes use of DBI package and thus supports several database backends, including mysql and postgresql.

See https://github.com/PredictiveEcology/SpaDES/wiki/Using-alternate-database-backends-for-Cache for further information on configuring these additional backends.

3 Reproducible workflow

In general, we feel that a liberal use of Cache will make a re-usable and reproducible work flow. shiny apps can be made, taking advantage of Cache. Indeed, much of the difficulty in managing data sets and saving them for future use, can be accommodated by caching.