Introduction to Cache

Eliot J. B. McIntire

November 21 2023

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 fastdigest::fastdigest on R memory objects and digest::digest on 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.042   0.000   0.043
# 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())
  })
})
##    user  system elapsed 
##   0.216   0.000   0.231
# 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, ea0cc279ec89614b.rds))
##      loaded memoised result from previous terra::project call
##    user  system elapsed 
##   0.050   0.000   0.057
options(opts)

all.equal(map1, map2, check.attributes = FALSE) # TRUE
##  [1] "Attributes: < Names: 2 string mismatches >"                                                           
##  [2] "Attributes: < Length mismatch: comparison on first 3 components >"                                    
##  [3] "Attributes: < Component \".Cache\": Component \"newCache\": 1 element mismatch >"                     
##  [4] "Attributes: < Component 2: Attributes: < target is NULL, current is list > >"                         
##  [5] "Attributes: < Component 2: 1 string mismatch >"                                                       
##  [6] "Attributes: < Component 3: Modes: character, S4 >"                                                    
##  [7] "Attributes: < Component 3: names for current but not for target >"                                    
##  [8] "Attributes: < Component 3: Attributes: < Names: 1 string mismatch > >"                                
##  [9] "Attributes: < Component 3: Attributes: < Length mismatch: comparison on first 1 components > >"       
## [10] "Attributes: < Component 3: Attributes: < Component 1: Modes: character, environment > >"              
## [11] "Attributes: < Component 3: Attributes: < Component 1: Lengths: 1, 29 > >"                             
## [12] "Attributes: < Component 3: Attributes: < Component 1: names for current but not for target > >"       
## [13] "Attributes: < Component 3: Attributes: < Component 1: target is character, current is environment > >"
## [14] "Attributes: < Component 3: target is character, current is Rcpp_SpatRaster >"

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)

# 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

1.3.1.1 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")

showCache(tmpDir, userTags = c("objectName"))
## Cache size:
##   Total (including Rasters): 504 bytes
##   Selected objects (not including Rasters): 504 bytes
##              cacheId              tagKey                   tagValue
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:10.860037
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.001533508 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.074242e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 26: deaa37372f85861b          objectName                          b
## 27: deaa37372f85861b            function                      runif
## 28: deaa37372f85861b               class                    numeric
## 29: deaa37372f85861b         object.size                       1008
## 30: deaa37372f85861b            accessed 2023-11-21 16:57:10.891676
## 31: deaa37372f85861b             inCloud                      FALSE
## 32: deaa37372f85861b            fromDisk                      FALSE
## 33: deaa37372f85861b          resultHash                           
## 34: deaa37372f85861b   elapsedTimeDigest           0.003335714 secs
## 35: deaa37372f85861b elapsedTimeFirstRun          1.978874e-05 secs
## 36: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 37: deaa37372f85861b      otherFunctions               process_file
## 38: deaa37372f85861b      otherFunctions              process_group
## 39: deaa37372f85861b      otherFunctions        process_group.block
## 40: deaa37372f85861b      otherFunctions                 call_block
## 41: deaa37372f85861b      otherFunctions                 block_exec
## 42: deaa37372f85861b      otherFunctions                      eng_r
## 43: deaa37372f85861b      otherFunctions               in_input_dir
## 44: deaa37372f85861b      otherFunctions                     in_dir
## 45: deaa37372f85861b      otherFunctions                  timing_fn
## 46: deaa37372f85861b      otherFunctions                     handle
## 47: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 48: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 49: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 50: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:10.860392
##  2: 2023-11-21 16:57:10.860392
##  3: 2023-11-21 16:57:10.860392
##  4: 2023-11-21 16:57:10.860392
##  5: 2023-11-21 16:57:10.860392
##  6: 2023-11-21 16:57:10.860392
##  7: 2023-11-21 16:57:10.860392
##  8: 2023-11-21 16:57:10.860392
##  9: 2023-11-21 16:57:10.860392
## 10: 2023-11-21 16:57:10.860392
## 11: 2023-11-21 16:57:10.860392
## 12: 2023-11-21 16:57:10.860392
## 13: 2023-11-21 16:57:10.860392
## 14: 2023-11-21 16:57:10.860392
## 15: 2023-11-21 16:57:10.860392
## 16: 2023-11-21 16:57:10.860392
## 17: 2023-11-21 16:57:10.860392
## 18: 2023-11-21 16:57:10.860392
## 19: 2023-11-21 16:57:10.860392
## 20: 2023-11-21 16:57:10.860392
## 21: 2023-11-21 16:57:10.860392
## 22: 2023-11-21 16:57:10.860392
## 23: 2023-11-21 16:57:10.860392
## 24: 2023-11-21 16:57:10.860392
## 25: 2023-11-21 16:57:10.860392
## 26: 2023-11-21 16:57:10.892029
## 27: 2023-11-21 16:57:10.892029
## 28: 2023-11-21 16:57:10.892029
## 29: 2023-11-21 16:57:10.892029
## 30: 2023-11-21 16:57:10.892029
## 31: 2023-11-21 16:57:10.892029
## 32: 2023-11-21 16:57:10.892029
## 33: 2023-11-21 16:57:10.892029
## 34: 2023-11-21 16:57:10.892029
## 35: 2023-11-21 16:57:10.892029
## 36: 2023-11-21 16:57:10.892029
## 37: 2023-11-21 16:57:10.892029
## 38: 2023-11-21 16:57:10.892029
## 39: 2023-11-21 16:57:10.892029
## 40: 2023-11-21 16:57:10.892029
## 41: 2023-11-21 16:57:10.892029
## 42: 2023-11-21 16:57:10.892029
## 43: 2023-11-21 16:57:10.892029
## 44: 2023-11-21 16:57:10.892029
## 45: 2023-11-21 16:57:10.892029
## 46: 2023-11-21 16:57:10.892029
## 47: 2023-11-21 16:57:10.892029
## 48: 2023-11-21 16:57:10.892029
## 49: 2023-11-21 16:57:10.892029
## 50: 2023-11-21 16:57:10.892029
##                    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
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:10.860037
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.001533508 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.074242e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:10.860392
##  2: 2023-11-21 16:57:10.860392
##  3: 2023-11-21 16:57:10.860392
##  4: 2023-11-21 16:57:10.860392
##  5: 2023-11-21 16:57:10.860392
##  6: 2023-11-21 16:57:10.860392
##  7: 2023-11-21 16:57:10.860392
##  8: 2023-11-21 16:57:10.860392
##  9: 2023-11-21 16:57:10.860392
## 10: 2023-11-21 16:57:10.860392
## 11: 2023-11-21 16:57:10.860392
## 12: 2023-11-21 16:57:10.860392
## 13: 2023-11-21 16:57:10.860392
## 14: 2023-11-21 16:57:10.860392
## 15: 2023-11-21 16:57:10.860392
## 16: 2023-11-21 16:57:10.860392
## 17: 2023-11-21 16:57:10.860392
## 18: 2023-11-21 16:57:10.860392
## 19: 2023-11-21 16:57:10.860392
## 20: 2023-11-21 16:57:10.860392
## 21: 2023-11-21 16:57:10.860392
## 22: 2023-11-21 16:57:10.860392
## 23: 2023-11-21 16:57:10.860392
## 24: 2023-11-21 16:57:10.860392
## 25: 2023-11-21 16:57:10.860392
##                    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
##  1: deaa37372f85861b          objectName                          b
##  2: deaa37372f85861b            function                      runif
##  3: deaa37372f85861b               class                    numeric
##  4: deaa37372f85861b         object.size                       1008
##  5: deaa37372f85861b            accessed 2023-11-21 16:57:10.891676
##  6: deaa37372f85861b             inCloud                      FALSE
##  7: deaa37372f85861b            fromDisk                      FALSE
##  8: deaa37372f85861b          resultHash                           
##  9: deaa37372f85861b   elapsedTimeDigest           0.003335714 secs
## 10: deaa37372f85861b elapsedTimeFirstRun          1.978874e-05 secs
## 11: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 12: deaa37372f85861b      otherFunctions               process_file
## 13: deaa37372f85861b      otherFunctions              process_group
## 14: deaa37372f85861b      otherFunctions        process_group.block
## 15: deaa37372f85861b      otherFunctions                 call_block
## 16: deaa37372f85861b      otherFunctions                 block_exec
## 17: deaa37372f85861b      otherFunctions                      eng_r
## 18: deaa37372f85861b      otherFunctions               in_input_dir
## 19: deaa37372f85861b      otherFunctions                     in_dir
## 20: deaa37372f85861b      otherFunctions                  timing_fn
## 21: deaa37372f85861b      otherFunctions                     handle
## 22: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 23: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 24: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 25: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:10.892029
##  2: 2023-11-21 16:57:10.892029
##  3: 2023-11-21 16:57:10.892029
##  4: 2023-11-21 16:57:10.892029
##  5: 2023-11-21 16:57:10.892029
##  6: 2023-11-21 16:57:10.892029
##  7: 2023-11-21 16:57:10.892029
##  8: 2023-11-21 16:57:10.892029
##  9: 2023-11-21 16:57:10.892029
## 10: 2023-11-21 16:57:10.892029
## 11: 2023-11-21 16:57:10.892029
## 12: 2023-11-21 16:57:10.892029
## 13: 2023-11-21 16:57:10.892029
## 14: 2023-11-21 16:57:10.892029
## 15: 2023-11-21 16:57:10.892029
## 16: 2023-11-21 16:57:10.892029
## 17: 2023-11-21 16:57:10.892029
## 18: 2023-11-21 16:57:10.892029
## 19: 2023-11-21 16:57:10.892029
## 20: 2023-11-21 16:57:10.892029
## 21: 2023-11-21 16:57:10.892029
## 22: 2023-11-21 16:57:10.892029
## 23: 2023-11-21 16:57:10.892029
## 24: 2023-11-21 16:57:10.892029
## 25: 2023-11-21 16:57:10.892029
##                    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
##  1: 4fca280cda001fc9            function                      rnorm
##  2: 4fca280cda001fc9               class                    numeric
##  3: 4fca280cda001fc9         object.size                       1104
##  4: 4fca280cda001fc9            accessed  2023-11-21 16:57:10.74762
##  5: 4fca280cda001fc9             inCloud                      FALSE
##  6: 4fca280cda001fc9            fromDisk                      FALSE
##  7: 4fca280cda001fc9          resultHash                           
##  8: 4fca280cda001fc9   elapsedTimeDigest           0.001775026 secs
##  9: 4fca280cda001fc9 elapsedTimeFirstRun          3.099442e-05 secs
## 10: 4fca280cda001fc9      otherFunctions           vweave_rmarkdown
## 11: 4fca280cda001fc9      otherFunctions               process_file
## 12: 4fca280cda001fc9      otherFunctions              process_group
## 13: 4fca280cda001fc9      otherFunctions        process_group.block
## 14: 4fca280cda001fc9      otherFunctions                 call_block
## 15: 4fca280cda001fc9      otherFunctions                 block_exec
## 16: 4fca280cda001fc9      otherFunctions                      eng_r
## 17: 4fca280cda001fc9      otherFunctions               in_input_dir
## 18: 4fca280cda001fc9      otherFunctions                     in_dir
## 19: 4fca280cda001fc9      otherFunctions                  timing_fn
## 20: 4fca280cda001fc9      otherFunctions                     handle
## 21: 4fca280cda001fc9           preDigest         n:c5775c3b366fb719
## 22: 4fca280cda001fc9           preDigest      mean:15620f138033a66c
## 23: 4fca280cda001fc9           preDigest        sd:853b1797f54b229c
## 24: 4fca280cda001fc9           preDigest      .FUN:4f604aa46882b368
## 25: 4fca280cda001fc9            accessed 2023-11-21 16:57:10.767066
## 26: 4fca280cda001fc9     elapsedTimeLoad           0.005263805 secs
## 27: 4fca280cda001fc9            accessed  2023-11-21 16:57:10.78423
## 28: 4fca280cda001fc9            accessed  2023-11-21 16:57:10.80131
## 29: 4fca280cda001fc9            accessed 2023-11-21 16:57:10.818319
## 30: 5efb386c43c29ce1            function                      rnorm
## 31: 5efb386c43c29ce1               class                    numeric
## 32: 5efb386c43c29ce1         object.size                       1104
## 33: 5efb386c43c29ce1            accessed  2023-11-21 16:57:10.83457
## 34: 5efb386c43c29ce1             inCloud                      FALSE
## 35: 5efb386c43c29ce1            fromDisk                      FALSE
## 36: 5efb386c43c29ce1          resultHash                           
## 37: 5efb386c43c29ce1   elapsedTimeDigest           0.001378775 secs
## 38: 5efb386c43c29ce1 elapsedTimeFirstRun            2.0504e-05 secs
## 39: 5efb386c43c29ce1      otherFunctions           vweave_rmarkdown
## 40: 5efb386c43c29ce1      otherFunctions               process_file
## 41: 5efb386c43c29ce1      otherFunctions              process_group
## 42: 5efb386c43c29ce1      otherFunctions        process_group.block
## 43: 5efb386c43c29ce1      otherFunctions                 call_block
## 44: 5efb386c43c29ce1      otherFunctions                 block_exec
## 45: 5efb386c43c29ce1      otherFunctions                      eng_r
## 46: 5efb386c43c29ce1      otherFunctions               in_input_dir
## 47: 5efb386c43c29ce1      otherFunctions                     in_dir
## 48: 5efb386c43c29ce1      otherFunctions                  timing_fn
## 49: 5efb386c43c29ce1      otherFunctions                     handle
## 50: 5efb386c43c29ce1           preDigest         n:c5775c3b366fb719
## 51: 5efb386c43c29ce1           preDigest      mean:152602b8ff81e5bb
## 52: 5efb386c43c29ce1           preDigest        sd:853b1797f54b229c
## 53: 5efb386c43c29ce1           preDigest      .FUN:4f604aa46882b368
## 54: ad0ea27476c50b66          objectName                          a
## 55: ad0ea27476c50b66            function                      rnorm
## 56: ad0ea27476c50b66               class                    numeric
## 57: ad0ea27476c50b66         object.size                       1008
## 58: ad0ea27476c50b66            accessed 2023-11-21 16:57:10.860037
## 59: ad0ea27476c50b66             inCloud                      FALSE
## 60: ad0ea27476c50b66            fromDisk                      FALSE
## 61: ad0ea27476c50b66          resultHash                           
## 62: ad0ea27476c50b66   elapsedTimeDigest           0.001533508 secs
## 63: ad0ea27476c50b66 elapsedTimeFirstRun          2.074242e-05 secs
## 64: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 65: ad0ea27476c50b66      otherFunctions               process_file
## 66: ad0ea27476c50b66      otherFunctions              process_group
## 67: ad0ea27476c50b66      otherFunctions        process_group.block
## 68: ad0ea27476c50b66      otherFunctions                 call_block
## 69: ad0ea27476c50b66      otherFunctions                 block_exec
## 70: ad0ea27476c50b66      otherFunctions                      eng_r
## 71: ad0ea27476c50b66      otherFunctions               in_input_dir
## 72: ad0ea27476c50b66      otherFunctions                     in_dir
## 73: ad0ea27476c50b66      otherFunctions                  timing_fn
## 74: ad0ea27476c50b66      otherFunctions                     handle
## 75: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 76: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 77: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 78: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:10.747955
##  2: 2023-11-21 16:57:10.747955
##  3: 2023-11-21 16:57:10.747955
##  4: 2023-11-21 16:57:10.747955
##  5: 2023-11-21 16:57:10.747955
##  6: 2023-11-21 16:57:10.747955
##  7: 2023-11-21 16:57:10.747955
##  8: 2023-11-21 16:57:10.747955
##  9: 2023-11-21 16:57:10.747955
## 10: 2023-11-21 16:57:10.747955
## 11: 2023-11-21 16:57:10.747955
## 12: 2023-11-21 16:57:10.747955
## 13: 2023-11-21 16:57:10.747955
## 14: 2023-11-21 16:57:10.747955
## 15: 2023-11-21 16:57:10.747955
## 16: 2023-11-21 16:57:10.747955
## 17: 2023-11-21 16:57:10.747955
## 18: 2023-11-21 16:57:10.747955
## 19: 2023-11-21 16:57:10.747955
## 20: 2023-11-21 16:57:10.747955
## 21: 2023-11-21 16:57:10.747955
## 22: 2023-11-21 16:57:10.747955
## 23: 2023-11-21 16:57:10.747955
## 24: 2023-11-21 16:57:10.747955
## 25: 2023-11-21 16:57:10.767066
## 26:  2023-11-21 16:57:10.77279
## 27:  2023-11-21 16:57:10.78423
## 28:  2023-11-21 16:57:10.80131
## 29: 2023-11-21 16:57:10.818319
## 30: 2023-11-21 16:57:10.834902
## 31: 2023-11-21 16:57:10.834902
## 32: 2023-11-21 16:57:10.834902
## 33: 2023-11-21 16:57:10.834902
## 34: 2023-11-21 16:57:10.834902
## 35: 2023-11-21 16:57:10.834902
## 36: 2023-11-21 16:57:10.834902
## 37: 2023-11-21 16:57:10.834902
## 38: 2023-11-21 16:57:10.834902
## 39: 2023-11-21 16:57:10.834902
## 40: 2023-11-21 16:57:10.834902
## 41: 2023-11-21 16:57:10.834902
## 42: 2023-11-21 16:57:10.834902
## 43: 2023-11-21 16:57:10.834902
## 44: 2023-11-21 16:57:10.834902
## 45: 2023-11-21 16:57:10.834902
## 46: 2023-11-21 16:57:10.834902
## 47: 2023-11-21 16:57:10.834902
## 48: 2023-11-21 16:57:10.834902
## 49: 2023-11-21 16:57:10.834902
## 50: 2023-11-21 16:57:10.834902
## 51: 2023-11-21 16:57:10.834902
## 52: 2023-11-21 16:57:10.834902
## 53: 2023-11-21 16:57:10.834902
## 54: 2023-11-21 16:57:10.860392
## 55: 2023-11-21 16:57:10.860392
## 56: 2023-11-21 16:57:10.860392
## 57: 2023-11-21 16:57:10.860392
## 58: 2023-11-21 16:57:10.860392
## 59: 2023-11-21 16:57:10.860392
## 60: 2023-11-21 16:57:10.860392
## 61: 2023-11-21 16:57:10.860392
## 62: 2023-11-21 16:57:10.860392
## 63: 2023-11-21 16:57:10.860392
## 64: 2023-11-21 16:57:10.860392
## 65: 2023-11-21 16:57:10.860392
## 66: 2023-11-21 16:57:10.860392
## 67: 2023-11-21 16:57:10.860392
## 68: 2023-11-21 16:57:10.860392
## 69: 2023-11-21 16:57:10.860392
## 70: 2023-11-21 16:57:10.860392
## 71: 2023-11-21 16:57:10.860392
## 72: 2023-11-21 16:57:10.860392
## 73: 2023-11-21 16:57:10.860392
## 74: 2023-11-21 16:57:10.860392
## 75: 2023-11-21 16:57:10.860392
## 76: 2023-11-21 16:57:10.860392
## 77: 2023-11-21 16:57:10.860392
## 78: 2023-11-21 16:57:10.860392
##                    createdDate
clearCache(tmpDir, ask = FALSE)

1.3.1.2 Example 2: using the “accessed” tag

ranNumsA <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:a")
ranNumsB <- Cache(runif, 4, cachePath = tmpDir, userTags = "objectName:b")

# 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
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:10.998515
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest            0.00135088 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.264977e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 26: ad0ea27476c50b66            accessed 2023-11-21 16:57:12.033401
## 27: ad0ea27476c50b66     elapsedTimeLoad            0.01582766 secs
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:10.998911
##  2: 2023-11-21 16:57:10.998911
##  3: 2023-11-21 16:57:10.998911
##  4: 2023-11-21 16:57:10.998911
##  5: 2023-11-21 16:57:10.998911
##  6: 2023-11-21 16:57:10.998911
##  7: 2023-11-21 16:57:10.998911
##  8: 2023-11-21 16:57:10.998911
##  9: 2023-11-21 16:57:10.998911
## 10: 2023-11-21 16:57:10.998911
## 11: 2023-11-21 16:57:10.998911
## 12: 2023-11-21 16:57:10.998911
## 13: 2023-11-21 16:57:10.998911
## 14: 2023-11-21 16:57:10.998911
## 15: 2023-11-21 16:57:10.998911
## 16: 2023-11-21 16:57:10.998911
## 17: 2023-11-21 16:57:10.998911
## 18: 2023-11-21 16:57:10.998911
## 19: 2023-11-21 16:57:10.998911
## 20: 2023-11-21 16:57:10.998911
## 21: 2023-11-21 16:57:10.998911
## 22: 2023-11-21 16:57:10.998911
## 23: 2023-11-21 16:57:10.998911
## 24: 2023-11-21 16:57:10.998911
## 25: 2023-11-21 16:57:10.998911
## 26: 2023-11-21 16:57:12.033401
## 27: 2023-11-21 16:57:12.049626
##                    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")

# 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
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:10.998515
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest            0.00135088 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.264977e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 26: ad0ea27476c50b66            accessed 2023-11-21 16:57:12.033401
## 27: ad0ea27476c50b66     elapsedTimeLoad           0.005691051 secs
## 28: ad0ea27476c50b66            accessed 2023-11-21 16:57:12.107337
## 29: deaa37372f85861b          objectName                          b
## 30: deaa37372f85861b            function                      runif
## 31: deaa37372f85861b               class                    numeric
## 32: deaa37372f85861b         object.size                       1008
## 33: deaa37372f85861b            accessed 2023-11-21 16:57:12.132337
## 34: deaa37372f85861b             inCloud                      FALSE
## 35: deaa37372f85861b            fromDisk                      FALSE
## 36: deaa37372f85861b          resultHash                           
## 37: deaa37372f85861b   elapsedTimeDigest           0.003813267 secs
## 38: deaa37372f85861b elapsedTimeFirstRun          3.600121e-05 secs
## 39: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 40: deaa37372f85861b      otherFunctions               process_file
## 41: deaa37372f85861b      otherFunctions              process_group
## 42: deaa37372f85861b      otherFunctions        process_group.block
## 43: deaa37372f85861b      otherFunctions                 call_block
## 44: deaa37372f85861b      otherFunctions                 block_exec
## 45: deaa37372f85861b      otherFunctions                      eng_r
## 46: deaa37372f85861b      otherFunctions               in_input_dir
## 47: deaa37372f85861b      otherFunctions                     in_dir
## 48: deaa37372f85861b      otherFunctions                  timing_fn
## 49: deaa37372f85861b      otherFunctions                     handle
## 50: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 51: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 52: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 53: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:10.998911
##  2: 2023-11-21 16:57:10.998911
##  3: 2023-11-21 16:57:10.998911
##  4: 2023-11-21 16:57:10.998911
##  5: 2023-11-21 16:57:10.998911
##  6: 2023-11-21 16:57:10.998911
##  7: 2023-11-21 16:57:10.998911
##  8: 2023-11-21 16:57:10.998911
##  9: 2023-11-21 16:57:10.998911
## 10: 2023-11-21 16:57:10.998911
## 11: 2023-11-21 16:57:10.998911
## 12: 2023-11-21 16:57:10.998911
## 13: 2023-11-21 16:57:10.998911
## 14: 2023-11-21 16:57:10.998911
## 15: 2023-11-21 16:57:10.998911
## 16: 2023-11-21 16:57:10.998911
## 17: 2023-11-21 16:57:10.998911
## 18: 2023-11-21 16:57:10.998911
## 19: 2023-11-21 16:57:10.998911
## 20: 2023-11-21 16:57:10.998911
## 21: 2023-11-21 16:57:10.998911
## 22: 2023-11-21 16:57:10.998911
## 23: 2023-11-21 16:57:10.998911
## 24: 2023-11-21 16:57:10.998911
## 25: 2023-11-21 16:57:10.998911
## 26: 2023-11-21 16:57:12.033401
## 27: 2023-11-21 16:57:12.049626
## 28: 2023-11-21 16:57:12.107337
## 29: 2023-11-21 16:57:12.132963
## 30: 2023-11-21 16:57:12.132963
## 31: 2023-11-21 16:57:12.132963
## 32: 2023-11-21 16:57:12.132963
## 33: 2023-11-21 16:57:12.132963
## 34: 2023-11-21 16:57:12.132963
## 35: 2023-11-21 16:57:12.132963
## 36: 2023-11-21 16:57:12.132963
## 37: 2023-11-21 16:57:12.132963
## 38: 2023-11-21 16:57:12.132963
## 39: 2023-11-21 16:57:12.132963
## 40: 2023-11-21 16:57:12.132963
## 41: 2023-11-21 16:57:12.132963
## 42: 2023-11-21 16:57:12.132963
## 43: 2023-11-21 16:57:12.132963
## 44: 2023-11-21 16:57:12.132963
## 45: 2023-11-21 16:57:12.132963
## 46: 2023-11-21 16:57:12.132963
## 47: 2023-11-21 16:57:12.132963
## 48: 2023-11-21 16:57:12.132963
## 49: 2023-11-21 16:57:12.132963
## 50: 2023-11-21 16:57:12.132963
## 51: 2023-11-21 16:57:12.132963
## 52: 2023-11-21 16:57:12.132963
## 53: 2023-11-21 16:57:12.132963
##                    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
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:10.998515
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest            0.00135088 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.264977e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 26: ad0ea27476c50b66            accessed 2023-11-21 16:57:12.033401
## 27: ad0ea27476c50b66     elapsedTimeLoad           0.005691051 secs
## 28: ad0ea27476c50b66            accessed 2023-11-21 16:57:12.107337
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:10.998911
##  2: 2023-11-21 16:57:10.998911
##  3: 2023-11-21 16:57:10.998911
##  4: 2023-11-21 16:57:10.998911
##  5: 2023-11-21 16:57:10.998911
##  6: 2023-11-21 16:57:10.998911
##  7: 2023-11-21 16:57:10.998911
##  8: 2023-11-21 16:57:10.998911
##  9: 2023-11-21 16:57:10.998911
## 10: 2023-11-21 16:57:10.998911
## 11: 2023-11-21 16:57:10.998911
## 12: 2023-11-21 16:57:10.998911
## 13: 2023-11-21 16:57:10.998911
## 14: 2023-11-21 16:57:10.998911
## 15: 2023-11-21 16:57:10.998911
## 16: 2023-11-21 16:57:10.998911
## 17: 2023-11-21 16:57:10.998911
## 18: 2023-11-21 16:57:10.998911
## 19: 2023-11-21 16:57:10.998911
## 20: 2023-11-21 16:57:10.998911
## 21: 2023-11-21 16:57:10.998911
## 22: 2023-11-21 16:57:10.998911
## 23: 2023-11-21 16:57:10.998911
## 24: 2023-11-21 16:57:10.998911
## 25: 2023-11-21 16:57:10.998911
## 26: 2023-11-21 16:57:12.033401
## 27: 2023-11-21 16:57:12.049626
## 28: 2023-11-21 16:57:12.107337
##                    createdDate
showCache(tmpDir)
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##  1: ad0ea27476c50b66          objectName                          a
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:10.998515
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest            0.00135088 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.264977e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 26: ad0ea27476c50b66            accessed 2023-11-21 16:57:12.033401
## 27: ad0ea27476c50b66     elapsedTimeLoad           0.005691051 secs
## 28: ad0ea27476c50b66            accessed 2023-11-21 16:57:12.107337
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:10.998911
##  2: 2023-11-21 16:57:10.998911
##  3: 2023-11-21 16:57:10.998911
##  4: 2023-11-21 16:57:10.998911
##  5: 2023-11-21 16:57:10.998911
##  6: 2023-11-21 16:57:10.998911
##  7: 2023-11-21 16:57:10.998911
##  8: 2023-11-21 16:57:10.998911
##  9: 2023-11-21 16:57:10.998911
## 10: 2023-11-21 16:57:10.998911
## 11: 2023-11-21 16:57:10.998911
## 12: 2023-11-21 16:57:10.998911
## 13: 2023-11-21 16:57:10.998911
## 14: 2023-11-21 16:57:10.998911
## 15: 2023-11-21 16:57:10.998911
## 16: 2023-11-21 16:57:10.998911
## 17: 2023-11-21 16:57:10.998911
## 18: 2023-11-21 16:57:10.998911
## 19: 2023-11-21 16:57:10.998911
## 20: 2023-11-21 16:57:10.998911
## 21: 2023-11-21 16:57:10.998911
## 22: 2023-11-21 16:57:10.998911
## 23: 2023-11-21 16:57:10.998911
## 24: 2023-11-21 16:57:10.998911
## 25: 2023-11-21 16:57:10.998911
## 26: 2023-11-21 16:57:12.033401
## 27: 2023-11-21 16:57:12.049626
## 28: 2023-11-21 16:57:12.107337
##                    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")
startTime <- Sys.time()
Sys.sleep(1)
ranNumsB <- Cache(rnorm, 5, cachePath = tmpDir, userTags = "objectName:b")
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
##  1: ccacbf62081a42b4          objectName                          b
##  2: ccacbf62081a42b4            function                      rnorm
##  3: ccacbf62081a42b4               class                    numeric
##  4: ccacbf62081a42b4         object.size                       1024
##  5: ccacbf62081a42b4            accessed 2023-11-21 16:57:13.225784
##  6: ccacbf62081a42b4             inCloud                      FALSE
##  7: ccacbf62081a42b4            fromDisk                      FALSE
##  8: ccacbf62081a42b4          resultHash                           
##  9: ccacbf62081a42b4   elapsedTimeDigest           0.001696348 secs
## 10: ccacbf62081a42b4 elapsedTimeFirstRun          3.194809e-05 secs
## 11: ccacbf62081a42b4      otherFunctions           vweave_rmarkdown
## 12: ccacbf62081a42b4      otherFunctions               process_file
## 13: ccacbf62081a42b4      otherFunctions              process_group
## 14: ccacbf62081a42b4      otherFunctions        process_group.block
## 15: ccacbf62081a42b4      otherFunctions                 call_block
## 16: ccacbf62081a42b4      otherFunctions                 block_exec
## 17: ccacbf62081a42b4      otherFunctions                      eng_r
## 18: ccacbf62081a42b4      otherFunctions               in_input_dir
## 19: ccacbf62081a42b4      otherFunctions                     in_dir
## 20: ccacbf62081a42b4      otherFunctions                  timing_fn
## 21: ccacbf62081a42b4      otherFunctions                     handle
## 22: ccacbf62081a42b4           preDigest         n:a4f076b3db622faf
## 23: ccacbf62081a42b4           preDigest      mean:c40c00762a0dac94
## 24: ccacbf62081a42b4           preDigest        sd:853b1797f54b229c
## 25: ccacbf62081a42b4           preDigest      .FUN:4f604aa46882b368
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:13.226115
##  2: 2023-11-21 16:57:13.226115
##  3: 2023-11-21 16:57:13.226115
##  4: 2023-11-21 16:57:13.226115
##  5: 2023-11-21 16:57:13.226115
##  6: 2023-11-21 16:57:13.226115
##  7: 2023-11-21 16:57:13.226115
##  8: 2023-11-21 16:57:13.226115
##  9: 2023-11-21 16:57:13.226115
## 10: 2023-11-21 16:57:13.226115
## 11: 2023-11-21 16:57:13.226115
## 12: 2023-11-21 16:57:13.226115
## 13: 2023-11-21 16:57:13.226115
## 14: 2023-11-21 16:57:13.226115
## 15: 2023-11-21 16:57:13.226115
## 16: 2023-11-21 16:57:13.226115
## 17: 2023-11-21 16:57:13.226115
## 18: 2023-11-21 16:57:13.226115
## 19: 2023-11-21 16:57:13.226115
## 20: 2023-11-21 16:57:13.226115
## 21: 2023-11-21 16:57:13.226115
## 22: 2023-11-21 16:57:13.226115
## 23: 2023-11-21 16:57:13.226115
## 24: 2023-11-21 16:57:13.226115
## 25: 2023-11-21 16:57:13.226115
##                    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")
ranNumsB <- Cache(rnorm, 4, cachePath = tmpDir, userTags = "objectName:b")

# 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
##  1: ad0ea27476c50b66          objectName                          b
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:13.323643
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.001604795 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.026558e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 26: deaa37372f85861b          objectName                          a
## 27: deaa37372f85861b            function                      runif
## 28: deaa37372f85861b               class                    numeric
## 29: deaa37372f85861b         object.size                       1008
## 30: deaa37372f85861b            accessed 2023-11-21 16:57:13.307731
## 31: deaa37372f85861b             inCloud                      FALSE
## 32: deaa37372f85861b            fromDisk                      FALSE
## 33: deaa37372f85861b          resultHash                           
## 34: deaa37372f85861b   elapsedTimeDigest           0.001680851 secs
## 35: deaa37372f85861b elapsedTimeFirstRun          1.978874e-05 secs
## 36: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 37: deaa37372f85861b      otherFunctions               process_file
## 38: deaa37372f85861b      otherFunctions              process_group
## 39: deaa37372f85861b      otherFunctions        process_group.block
## 40: deaa37372f85861b      otherFunctions                 call_block
## 41: deaa37372f85861b      otherFunctions                 block_exec
## 42: deaa37372f85861b      otherFunctions                      eng_r
## 43: deaa37372f85861b      otherFunctions               in_input_dir
## 44: deaa37372f85861b      otherFunctions                     in_dir
## 45: deaa37372f85861b      otherFunctions                  timing_fn
## 46: deaa37372f85861b      otherFunctions                     handle
## 47: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 48: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 49: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 50: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:13.323972
##  2: 2023-11-21 16:57:13.323972
##  3: 2023-11-21 16:57:13.323972
##  4: 2023-11-21 16:57:13.323972
##  5: 2023-11-21 16:57:13.323972
##  6: 2023-11-21 16:57:13.323972
##  7: 2023-11-21 16:57:13.323972
##  8: 2023-11-21 16:57:13.323972
##  9: 2023-11-21 16:57:13.323972
## 10: 2023-11-21 16:57:13.323972
## 11: 2023-11-21 16:57:13.323972
## 12: 2023-11-21 16:57:13.323972
## 13: 2023-11-21 16:57:13.323972
## 14: 2023-11-21 16:57:13.323972
## 15: 2023-11-21 16:57:13.323972
## 16: 2023-11-21 16:57:13.323972
## 17: 2023-11-21 16:57:13.323972
## 18: 2023-11-21 16:57:13.323972
## 19: 2023-11-21 16:57:13.323972
## 20: 2023-11-21 16:57:13.323972
## 21: 2023-11-21 16:57:13.323972
## 22: 2023-11-21 16:57:13.323972
## 23: 2023-11-21 16:57:13.323972
## 24: 2023-11-21 16:57:13.323972
## 25: 2023-11-21 16:57:13.323972
## 26: 2023-11-21 16:57:13.308062
## 27: 2023-11-21 16:57:13.308062
## 28: 2023-11-21 16:57:13.308062
## 29: 2023-11-21 16:57:13.308062
## 30: 2023-11-21 16:57:13.308062
## 31: 2023-11-21 16:57:13.308062
## 32: 2023-11-21 16:57:13.308062
## 33: 2023-11-21 16:57:13.308062
## 34: 2023-11-21 16:57:13.308062
## 35: 2023-11-21 16:57:13.308062
## 36: 2023-11-21 16:57:13.308062
## 37: 2023-11-21 16:57:13.308062
## 38: 2023-11-21 16:57:13.308062
## 39: 2023-11-21 16:57:13.308062
## 40: 2023-11-21 16:57:13.308062
## 41: 2023-11-21 16:57:13.308062
## 42: 2023-11-21 16:57:13.308062
## 43: 2023-11-21 16:57:13.308062
## 44: 2023-11-21 16:57:13.308062
## 45: 2023-11-21 16:57:13.308062
## 46: 2023-11-21 16:57:13.308062
## 47: 2023-11-21 16:57:13.308062
## 48: 2023-11-21 16:57:13.308062
## 49: 2023-11-21 16:57:13.308062
## 50: 2023-11-21 16:57:13.308062
##                    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
##  1: ad0ea27476c50b66          objectName                          b
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:13.323643
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.001604795 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.026558e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 26: deaa37372f85861b          objectName                          a
## 27: deaa37372f85861b            function                      runif
## 28: deaa37372f85861b               class                    numeric
## 29: deaa37372f85861b         object.size                       1008
## 30: deaa37372f85861b            accessed 2023-11-21 16:57:13.307731
## 31: deaa37372f85861b             inCloud                      FALSE
## 32: deaa37372f85861b            fromDisk                      FALSE
## 33: deaa37372f85861b          resultHash                           
## 34: deaa37372f85861b   elapsedTimeDigest           0.001680851 secs
## 35: deaa37372f85861b elapsedTimeFirstRun          1.978874e-05 secs
## 36: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 37: deaa37372f85861b      otherFunctions               process_file
## 38: deaa37372f85861b      otherFunctions              process_group
## 39: deaa37372f85861b      otherFunctions        process_group.block
## 40: deaa37372f85861b      otherFunctions                 call_block
## 41: deaa37372f85861b      otherFunctions                 block_exec
## 42: deaa37372f85861b      otherFunctions                      eng_r
## 43: deaa37372f85861b      otherFunctions               in_input_dir
## 44: deaa37372f85861b      otherFunctions                     in_dir
## 45: deaa37372f85861b      otherFunctions                  timing_fn
## 46: deaa37372f85861b      otherFunctions                     handle
## 47: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 48: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 49: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 50: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:13.323972
##  2: 2023-11-21 16:57:13.323972
##  3: 2023-11-21 16:57:13.323972
##  4: 2023-11-21 16:57:13.323972
##  5: 2023-11-21 16:57:13.323972
##  6: 2023-11-21 16:57:13.323972
##  7: 2023-11-21 16:57:13.323972
##  8: 2023-11-21 16:57:13.323972
##  9: 2023-11-21 16:57:13.323972
## 10: 2023-11-21 16:57:13.323972
## 11: 2023-11-21 16:57:13.323972
## 12: 2023-11-21 16:57:13.323972
## 13: 2023-11-21 16:57:13.323972
## 14: 2023-11-21 16:57:13.323972
## 15: 2023-11-21 16:57:13.323972
## 16: 2023-11-21 16:57:13.323972
## 17: 2023-11-21 16:57:13.323972
## 18: 2023-11-21 16:57:13.323972
## 19: 2023-11-21 16:57:13.323972
## 20: 2023-11-21 16:57:13.323972
## 21: 2023-11-21 16:57:13.323972
## 22: 2023-11-21 16:57:13.323972
## 23: 2023-11-21 16:57:13.323972
## 24: 2023-11-21 16:57:13.323972
## 25: 2023-11-21 16:57:13.323972
## 26: 2023-11-21 16:57:13.308062
## 27: 2023-11-21 16:57:13.308062
## 28: 2023-11-21 16:57:13.308062
## 29: 2023-11-21 16:57:13.308062
## 30: 2023-11-21 16:57:13.308062
## 31: 2023-11-21 16:57:13.308062
## 32: 2023-11-21 16:57:13.308062
## 33: 2023-11-21 16:57:13.308062
## 34: 2023-11-21 16:57:13.308062
## 35: 2023-11-21 16:57:13.308062
## 36: 2023-11-21 16:57:13.308062
## 37: 2023-11-21 16:57:13.308062
## 38: 2023-11-21 16:57:13.308062
## 39: 2023-11-21 16:57:13.308062
## 40: 2023-11-21 16:57:13.308062
## 41: 2023-11-21 16:57:13.308062
## 42: 2023-11-21 16:57:13.308062
## 43: 2023-11-21 16:57:13.308062
## 44: 2023-11-21 16:57:13.308062
## 45: 2023-11-21 16:57:13.308062
## 46: 2023-11-21 16:57:13.308062
## 47: 2023-11-21 16:57:13.308062
## 48: 2023-11-21 16:57:13.308062
## 49: 2023-11-21 16:57:13.308062
## 50: 2023-11-21 16:57:13.308062
##                    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
##  1: ad0ea27476c50b66          objectName                          b
##  2: ad0ea27476c50b66            function                      rnorm
##  3: ad0ea27476c50b66               class                    numeric
##  4: ad0ea27476c50b66         object.size                       1008
##  5: ad0ea27476c50b66            accessed 2023-11-21 16:57:13.323643
##  6: ad0ea27476c50b66             inCloud                      FALSE
##  7: ad0ea27476c50b66            fromDisk                      FALSE
##  8: ad0ea27476c50b66          resultHash                           
##  9: ad0ea27476c50b66   elapsedTimeDigest           0.001604795 secs
## 10: ad0ea27476c50b66 elapsedTimeFirstRun          2.026558e-05 secs
## 11: ad0ea27476c50b66      otherFunctions           vweave_rmarkdown
## 12: ad0ea27476c50b66      otherFunctions               process_file
## 13: ad0ea27476c50b66      otherFunctions              process_group
## 14: ad0ea27476c50b66      otherFunctions        process_group.block
## 15: ad0ea27476c50b66      otherFunctions                 call_block
## 16: ad0ea27476c50b66      otherFunctions                 block_exec
## 17: ad0ea27476c50b66      otherFunctions                      eng_r
## 18: ad0ea27476c50b66      otherFunctions               in_input_dir
## 19: ad0ea27476c50b66      otherFunctions                     in_dir
## 20: ad0ea27476c50b66      otherFunctions                  timing_fn
## 21: ad0ea27476c50b66      otherFunctions                     handle
## 22: ad0ea27476c50b66           preDigest         n:7eef4eae85fd9229
## 23: ad0ea27476c50b66           preDigest      mean:c40c00762a0dac94
## 24: ad0ea27476c50b66           preDigest        sd:853b1797f54b229c
## 25: ad0ea27476c50b66           preDigest      .FUN:4f604aa46882b368
## 26: deaa37372f85861b          objectName                          a
## 27: deaa37372f85861b            function                      runif
## 28: deaa37372f85861b               class                    numeric
## 29: deaa37372f85861b         object.size                       1008
## 30: deaa37372f85861b            accessed 2023-11-21 16:57:13.307731
## 31: deaa37372f85861b             inCloud                      FALSE
## 32: deaa37372f85861b            fromDisk                      FALSE
## 33: deaa37372f85861b          resultHash                           
## 34: deaa37372f85861b   elapsedTimeDigest           0.001680851 secs
## 35: deaa37372f85861b elapsedTimeFirstRun          1.978874e-05 secs
## 36: deaa37372f85861b      otherFunctions           vweave_rmarkdown
## 37: deaa37372f85861b      otherFunctions               process_file
## 38: deaa37372f85861b      otherFunctions              process_group
## 39: deaa37372f85861b      otherFunctions        process_group.block
## 40: deaa37372f85861b      otherFunctions                 call_block
## 41: deaa37372f85861b      otherFunctions                 block_exec
## 42: deaa37372f85861b      otherFunctions                      eng_r
## 43: deaa37372f85861b      otherFunctions               in_input_dir
## 44: deaa37372f85861b      otherFunctions                     in_dir
## 45: deaa37372f85861b      otherFunctions                  timing_fn
## 46: deaa37372f85861b      otherFunctions                     handle
## 47: deaa37372f85861b           preDigest         n:7eef4eae85fd9229
## 48: deaa37372f85861b           preDigest       min:c40c00762a0dac94
## 49: deaa37372f85861b           preDigest       max:853b1797f54b229c
## 50: deaa37372f85861b           preDigest      .FUN:881ec847b7161f3c
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:13.323972
##  2: 2023-11-21 16:57:13.323972
##  3: 2023-11-21 16:57:13.323972
##  4: 2023-11-21 16:57:13.323972
##  5: 2023-11-21 16:57:13.323972
##  6: 2023-11-21 16:57:13.323972
##  7: 2023-11-21 16:57:13.323972
##  8: 2023-11-21 16:57:13.323972
##  9: 2023-11-21 16:57:13.323972
## 10: 2023-11-21 16:57:13.323972
## 11: 2023-11-21 16:57:13.323972
## 12: 2023-11-21 16:57:13.323972
## 13: 2023-11-21 16:57:13.323972
## 14: 2023-11-21 16:57:13.323972
## 15: 2023-11-21 16:57:13.323972
## 16: 2023-11-21 16:57:13.323972
## 17: 2023-11-21 16:57:13.323972
## 18: 2023-11-21 16:57:13.323972
## 19: 2023-11-21 16:57:13.323972
## 20: 2023-11-21 16:57:13.323972
## 21: 2023-11-21 16:57:13.323972
## 22: 2023-11-21 16:57:13.323972
## 23: 2023-11-21 16:57:13.323972
## 24: 2023-11-21 16:57:13.323972
## 25: 2023-11-21 16:57:13.323972
## 26: 2023-11-21 16:57:13.308062
## 27: 2023-11-21 16:57:13.308062
## 28: 2023-11-21 16:57:13.308062
## 29: 2023-11-21 16:57:13.308062
## 30: 2023-11-21 16:57:13.308062
## 31: 2023-11-21 16:57:13.308062
## 32: 2023-11-21 16:57:13.308062
## 33: 2023-11-21 16:57:13.308062
## 34: 2023-11-21 16:57:13.308062
## 35: 2023-11-21 16:57:13.308062
## 36: 2023-11-21 16:57:13.308062
## 37: 2023-11-21 16:57:13.308062
## 38: 2023-11-21 16:57:13.308062
## 39: 2023-11-21 16:57:13.308062
## 40: 2023-11-21 16:57:13.308062
## 41: 2023-11-21 16:57:13.308062
## 42: 2023-11-21 16:57:13.308062
## 43: 2023-11-21 16:57:13.308062
## 44: 2023-11-21 16:57:13.308062
## 45: 2023-11-21 16:57:13.308062
## 46: 2023-11-21 16:57:13.308062
## 47: 2023-11-21 16:57:13.308062
## 48: 2023-11-21 16:57:13.308062
## 49: 2023-11-21 16:57:13.308062
## 50: 2023-11-21 16:57:13.308062
##                    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)
)

# 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, 3670144ac5764525.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.
## [1] 1.3282005 0.8067617 0.6990692
## 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
##  1: dffc8e3bf8c23b6e            function                      outer
##  2: dffc8e3bf8c23b6e               class                    numeric
##  3: dffc8e3bf8c23b6e         object.size                       1008
##  4: dffc8e3bf8c23b6e            accessed 2023-11-21 16:57:13.589748
##  5: dffc8e3bf8c23b6e             inCloud                      FALSE
##  6: dffc8e3bf8c23b6e            fromDisk                      FALSE
##  7: dffc8e3bf8c23b6e          resultHash                           
##  8: dffc8e3bf8c23b6e   elapsedTimeDigest           0.001847744 secs
##  9: dffc8e3bf8c23b6e elapsedTimeFirstRun            0.05754375 secs
## 10: dffc8e3bf8c23b6e      otherFunctions           vweave_rmarkdown
## 11: dffc8e3bf8c23b6e      otherFunctions               process_file
## 12: dffc8e3bf8c23b6e      otherFunctions              process_group
## 13: dffc8e3bf8c23b6e      otherFunctions        process_group.block
## 14: dffc8e3bf8c23b6e      otherFunctions                 call_block
## 15: dffc8e3bf8c23b6e      otherFunctions                 block_exec
## 16: dffc8e3bf8c23b6e      otherFunctions                      eng_r
## 17: dffc8e3bf8c23b6e      otherFunctions               in_input_dir
## 18: dffc8e3bf8c23b6e      otherFunctions                     in_dir
## 19: dffc8e3bf8c23b6e      otherFunctions                  timing_fn
## 20: dffc8e3bf8c23b6e      otherFunctions                     handle
## 21: dffc8e3bf8c23b6e           preDigest         n:82dc709f2b91918a
## 22: dffc8e3bf8c23b6e           preDigest      .FUN:b89dd186387954a0
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:13.590079
##  2: 2023-11-21 16:57:13.590079
##  3: 2023-11-21 16:57:13.590079
##  4: 2023-11-21 16:57:13.590079
##  5: 2023-11-21 16:57:13.590079
##  6: 2023-11-21 16:57:13.590079
##  7: 2023-11-21 16:57:13.590079
##  8: 2023-11-21 16:57:13.590079
##  9: 2023-11-21 16:57:13.590079
## 10: 2023-11-21 16:57:13.590079
## 11: 2023-11-21 16:57:13.590079
## 12: 2023-11-21 16:57:13.590079
## 13: 2023-11-21 16:57:13.590079
## 14: 2023-11-21 16:57:13.590079
## 15: 2023-11-21 16:57:13.590079
## 16: 2023-11-21 16:57:13.590079
## 17: 2023-11-21 16:57:13.590079
## 18: 2023-11-21 16:57:13.590079
## 19: 2023-11-21 16:57:13.590079
## 20: 2023-11-21 16:57:13.590079
## 21: 2023-11-21 16:57:13.590079
## 22: 2023-11-21 16:57:13.590079
##                    createdDate
showCache(tmpdir2) # 1 function call
## Cache size:
##   Total (including Rasters): 252 bytes
##   Selected objects (not including Rasters): 252 bytes
##              cacheId              tagKey                   tagValue
##  1: 33ceb4fb525fd08f            function                      inner
##  2: 33ceb4fb525fd08f               class                    numeric
##  3: 33ceb4fb525fd08f         object.size                       1008
##  4: 33ceb4fb525fd08f            accessed 2023-11-21 16:57:13.581893
##  5: 33ceb4fb525fd08f             inCloud                      FALSE
##  6: 33ceb4fb525fd08f            fromDisk                      FALSE
##  7: 33ceb4fb525fd08f          resultHash                           
##  8: 33ceb4fb525fd08f   elapsedTimeDigest           0.001413584 secs
##  9: 33ceb4fb525fd08f elapsedTimeFirstRun            0.02748775 secs
## 10: 33ceb4fb525fd08f      otherFunctions           vweave_rmarkdown
## 11: 33ceb4fb525fd08f      otherFunctions               process_file
## 12: 33ceb4fb525fd08f      otherFunctions              process_group
## 13: 33ceb4fb525fd08f      otherFunctions        process_group.block
## 14: 33ceb4fb525fd08f      otherFunctions                 call_block
## 15: 33ceb4fb525fd08f      otherFunctions                 block_exec
## 16: 33ceb4fb525fd08f      otherFunctions                      eng_r
## 17: 33ceb4fb525fd08f      otherFunctions               in_input_dir
## 18: 33ceb4fb525fd08f      otherFunctions                     in_dir
## 19: 33ceb4fb525fd08f      otherFunctions                  timing_fn
## 20: 33ceb4fb525fd08f      otherFunctions                     handle
## 21: 33ceb4fb525fd08f      otherFunctions                        out
## 22: 33ceb4fb525fd08f           preDigest      mean:22413394efd9f6a3
## 23: 33ceb4fb525fd08f           preDigest      .FUN:87e2c30917a34d25
##              cacheId              tagKey                   tagValue
##                    createdDate
##  1: 2023-11-21 16:57:13.582263
##  2: 2023-11-21 16:57:13.582263
##  3: 2023-11-21 16:57:13.582263
##  4: 2023-11-21 16:57:13.582263
##  5: 2023-11-21 16:57:13.582263
##  6: 2023-11-21 16:57:13.582263
##  7: 2023-11-21 16:57:13.582263
##  8: 2023-11-21 16:57:13.582263
##  9: 2023-11-21 16:57:13.582263
## 10: 2023-11-21 16:57:13.582263
## 11: 2023-11-21 16:57:13.582263
## 12: 2023-11-21 16:57:13.582263
## 13: 2023-11-21 16:57:13.582263
## 14: 2023-11-21 16:57:13.582263
## 15: 2023-11-21 16:57:13.582263
## 16: 2023-11-21 16:57:13.582263
## 17: 2023-11-21 16:57:13.582263
## 18: 2023-11-21 16:57:13.582263
## 19: 2023-11-21 16:57:13.582263
## 20: 2023-11-21 16:57:13.582263
## 21: 2023-11-21 16:57:13.582263
## 22: 2023-11-21 16:57:13.582263
## 23: 2023-11-21 16:57:13.582263
##                    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
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
##  1: 88a34e1d033329e5            outerTag                   outerTag
##  2: 88a34e1d033329e5            function                      outer
##  3: 88a34e1d033329e5               class                    numeric
##  4: 88a34e1d033329e5         object.size                       1008
##  5: 88a34e1d033329e5            accessed 2023-11-21 16:57:13.765611
##  6: 88a34e1d033329e5             inCloud                      FALSE
##  7: 88a34e1d033329e5            fromDisk                      FALSE
##  8: 88a34e1d033329e5          resultHash                           
##  9: 88a34e1d033329e5   elapsedTimeDigest           0.001209497 secs
## 10: 88a34e1d033329e5 elapsedTimeFirstRun             0.1315858 secs
## 11: 88a34e1d033329e5      otherFunctions           vweave_rmarkdown
## 12: 88a34e1d033329e5      otherFunctions               process_file
## 13: 88a34e1d033329e5      otherFunctions              process_group
## 14: 88a34e1d033329e5      otherFunctions        process_group.block
## 15: 88a34e1d033329e5      otherFunctions                 call_block
## 16: 88a34e1d033329e5      otherFunctions                 block_exec
## 17: 88a34e1d033329e5      otherFunctions                      eng_r
## 18: 88a34e1d033329e5      otherFunctions               in_input_dir
## 19: 88a34e1d033329e5      otherFunctions                     in_dir
## 20: 88a34e1d033329e5      otherFunctions                  timing_fn
## 21: 88a34e1d033329e5      otherFunctions                     handle
## 22: 88a34e1d033329e5           preDigest         n:82dc709f2b91918a
## 23: 88a34e1d033329e5           preDigest      .FUN:5f06fb5fbffe9e3b
##              cacheId              tagKey                   tagValue
##                   createdDate
##  1: 2023-11-21 16:57:13.76595
##  2: 2023-11-21 16:57:13.76595
##  3: 2023-11-21 16:57:13.76595
##  4: 2023-11-21 16:57:13.76595
##  5: 2023-11-21 16:57:13.76595
##  6: 2023-11-21 16:57:13.76595
##  7: 2023-11-21 16:57:13.76595
##  8: 2023-11-21 16:57:13.76595
##  9: 2023-11-21 16:57:13.76595
## 10: 2023-11-21 16:57:13.76595
## 11: 2023-11-21 16:57:13.76595
## 12: 2023-11-21 16:57:13.76595
## 13: 2023-11-21 16:57:13.76595
## 14: 2023-11-21 16:57:13.76595
## 15: 2023-11-21 16:57:13.76595
## 16: 2023-11-21 16:57:13.76595
## 17: 2023-11-21 16:57:13.76595
## 18: 2023-11-21 16:57:13.76595
## 19: 2023-11-21 16:57:13.76595
## 20: 2023-11-21 16:57:13.76595
## 21: 2023-11-21 16:57:13.76595
## 22: 2023-11-21 16:57:13.76595
## 23: 2023-11-21 16:57:13.76595
##                   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)
## [1] -0.6264538
## 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] -0.6264538
## 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
## [1] 0.7383247 0.5757814
## 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): 774 bytes
##   Selected objects (not including Rasters): 774 bytes
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.