Introduction to Cache

Eliot J. B. McIntire

July 30 2018

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 uses 2 key the archivist functions saveToLocalRepo and loadFromLocalRepo, but does not use archivist::cache. Similar to archivist::cache, there is some reliance on digest::digest to determine whether the arguments are identical in subsequent iterations; however, it also uses fastdigest::fastdigest to make it substantially faster in many cases. 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 using: Cache(FUN = functionName, ...).

This will be a slight change to a function call, such as: projectRaster(raster, crs = crs(newRaster)) to Cache(projectRaster, raster, crs = crs(newRaster)).

This is particularly useful for expensive operations.

library(raster)
## Loading required package: sp
library(reproducible)

tmpDir <- file.path(tempdir(), "reproducible_examples", "Cache")
checkPath(tmpDir, create = TRUE)
## [1] "/tmp/RtmpWjt1oV/reproducible_examples/Cache"
ras <- raster(extent(0,1000,0,1000), vals = 1:1e6, res = 1)
crs(ras) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"

newCRS <- "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# No Cache
system.time(map1 <- projectRaster(ras, crs = newCRS))
##    user  system elapsed 
##   2.655   0.116   2.777
# With Cache -- a little slower the first time because saving to disk
system.time(map1 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir,
                         notOlderThan = Sys.time()))
##    user  system elapsed 
##   2.542   0.186   2.876
# vastly faster the second time
system.time(map2 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir))
##   loading cached result from previous projectRaster call, adding to memoised copy
##    user  system elapsed 
##   0.119   0.000   0.118
# even faster the third time
system.time(map3 <- Cache(projectRaster, ras, crs = newCRS, cacheRepo = tmpDir))
##   loading memoised result from previous projectRaster call.
##    user  system elapsed 
##   0.045   0.000   0.044
all.equal(map1, map2) # TRUE
## [1] TRUE
all.equal(map1, map3) # TRUE
## [1] TRUE

1.3 Caching examples

1.3.1 Basic use

library(raster)
# magrittr, if loaded, gives an error below
  try(detach("package:magrittr", unload = TRUE), silent = TRUE)

try(clearCache(tmpDir), silent = TRUE) # just to make sure it is clear
## Cache size:
##   Total (including Rasters): 1.9 Mb
##   Selected objects (not including Rasters): 1.9 Mb
ranNumsA <- Cache(rnorm, 10, 16, cacheRepo = tmpDir)

# All same
ranNumsB <- Cache(rnorm, 10, 16, cacheRepo = tmpDir) # recovers cached copy
##   loading cached result from previous rnorm call, adding to memoised copy
ranNumsC <- rnorm(10, 16) %>% Cache(cacheRepo = tmpDir) # recovers cached copy
##   loading memoised result from previous 'rnorm' pipe sequence call.
ranNumsD <- Cache(quote(rnorm(n = 10, 16)), cacheRepo = tmpDir) # recovers cached copy
##   loading memoised result from previous rnorm call.
# Any minor change makes it different
ranNumsE <- rnorm(10, 6) %>% Cache(cacheRepo = tmpDir) # different

1.4 Example 1: Basic cache use with tags

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

showCache(tmpDir, userTags = c("objectName"))
## Cache size:
##   Total (including Rasters): 1000 bytes
##   Selected objects (not including Rasters): 476 bytes
##                             artifact         tagKey
##  1: 0ae638888fe7f9f1c4e64b417365a1b7         format
##  2: 0ae638888fe7f9f1c4e64b417365a1b7           name
##  3: 0ae638888fe7f9f1c4e64b417365a1b7          class
##  4: 0ae638888fe7f9f1c4e64b417365a1b7           date
##  5: 0ae638888fe7f9f1c4e64b417365a1b7        cacheId
##  6: 0ae638888fe7f9f1c4e64b417365a1b7     objectName
##  7: 0ae638888fe7f9f1c4e64b417365a1b7       function
##  8: 0ae638888fe7f9f1c4e64b417365a1b7    object.size
##  9: 0ae638888fe7f9f1c4e64b417365a1b7       accessed
## 10: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 11: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 12: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 13: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 14: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 15: 0ae638888fe7f9f1c4e64b417365a1b7      preDigest
## 16: 0ae638888fe7f9f1c4e64b417365a1b7      preDigest
## 17: 6f53d4a1f0a148000f83cfc8f0a350d4         format
## 18: 6f53d4a1f0a148000f83cfc8f0a350d4           name
## 19: 6f53d4a1f0a148000f83cfc8f0a350d4          class
## 20: 6f53d4a1f0a148000f83cfc8f0a350d4           date
## 21: 6f53d4a1f0a148000f83cfc8f0a350d4        cacheId
## 22: 6f53d4a1f0a148000f83cfc8f0a350d4     objectName
## 23: 6f53d4a1f0a148000f83cfc8f0a350d4       function
## 24: 6f53d4a1f0a148000f83cfc8f0a350d4    object.size
## 25: 6f53d4a1f0a148000f83cfc8f0a350d4       accessed
## 26: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 27: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 28: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 29: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 30: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 31: 6f53d4a1f0a148000f83cfc8f0a350d4      preDigest
## 32: 6f53d4a1f0a148000f83cfc8f0a350d4      preDigest
##                             artifact         tagKey
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:24
##  2:      0ae638888fe7f9f1c4e64b417365a1b7 2018-07-30 12:15:24
##  3:                               numeric 2018-07-30 12:15:24
##  4:                   2018-07-30 12:15:24 2018-07-30 12:15:24
##  5:      e37bb635c97bc2eeecab63816b881bbc 2018-07-30 12:15:24
##  6:                                     b 2018-07-30 12:15:24
##  7:                                 runif 2018-07-30 12:15:24
##  8:                                   952 2018-07-30 12:15:24
##  9:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 10:                          process_file 2018-07-30 12:15:24
## 11:                         process_group 2018-07-30 12:15:24
## 12:                   process_group.block 2018-07-30 12:15:24
## 13:                            call_block 2018-07-30 12:15:24
## 14:                            block_exec 2018-07-30 12:15:24
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:24
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-07-30 12:15:24
## 17:                                   rda 2018-07-30 12:15:24
## 18:      6f53d4a1f0a148000f83cfc8f0a350d4 2018-07-30 12:15:24
## 19:                               numeric 2018-07-30 12:15:24
## 20:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 21:      85874f26b2e0c1ef689a7d379d275ebf 2018-07-30 12:15:24
## 22:                                     a 2018-07-30 12:15:24
## 23:                                 rnorm 2018-07-30 12:15:24
## 24:                                   952 2018-07-30 12:15:24
## 25:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 26:                          process_file 2018-07-30 12:15:24
## 27:                         process_group 2018-07-30 12:15:24
## 28:                   process_group.block 2018-07-30 12:15:24
## 29:                            call_block 2018-07-30 12:15:24
## 30:                            block_exec 2018-07-30 12:15:24
## 31:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:24
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:24
##                                  tagValue         createdDate
showCache(tmpDir, userTags = c("^a$")) # regular expression ... "a" exactly
## Cache size:
##   Total (including Rasters): 1000 bytes
##   Selected objects (not including Rasters): 238 bytes
##                             artifact         tagKey
##  1: 6f53d4a1f0a148000f83cfc8f0a350d4         format
##  2: 6f53d4a1f0a148000f83cfc8f0a350d4           name
##  3: 6f53d4a1f0a148000f83cfc8f0a350d4          class
##  4: 6f53d4a1f0a148000f83cfc8f0a350d4           date
##  5: 6f53d4a1f0a148000f83cfc8f0a350d4        cacheId
##  6: 6f53d4a1f0a148000f83cfc8f0a350d4     objectName
##  7: 6f53d4a1f0a148000f83cfc8f0a350d4       function
##  8: 6f53d4a1f0a148000f83cfc8f0a350d4    object.size
##  9: 6f53d4a1f0a148000f83cfc8f0a350d4       accessed
## 10: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 11: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 12: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 13: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 14: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 15: 6f53d4a1f0a148000f83cfc8f0a350d4      preDigest
## 16: 6f53d4a1f0a148000f83cfc8f0a350d4      preDigest
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:24
##  2:      6f53d4a1f0a148000f83cfc8f0a350d4 2018-07-30 12:15:24
##  3:                               numeric 2018-07-30 12:15:24
##  4:                   2018-07-30 12:15:24 2018-07-30 12:15:24
##  5:      85874f26b2e0c1ef689a7d379d275ebf 2018-07-30 12:15:24
##  6:                                     a 2018-07-30 12:15:24
##  7:                                 rnorm 2018-07-30 12:15:24
##  8:                                   952 2018-07-30 12:15:24
##  9:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 10:                          process_file 2018-07-30 12:15:24
## 11:                         process_group 2018-07-30 12:15:24
## 12:                   process_group.block 2018-07-30 12:15:24
## 13:                            call_block 2018-07-30 12:15:24
## 14:                            block_exec 2018-07-30 12:15:24
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:24
## 16: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:24
showCache(tmpDir, userTags = c("runif")) # show only cached objects made during runif call
## Cache size:
##   Total (including Rasters): 1000 bytes
##   Selected objects (not including Rasters): 238 bytes
##                             artifact         tagKey
##  1: 0ae638888fe7f9f1c4e64b417365a1b7         format
##  2: 0ae638888fe7f9f1c4e64b417365a1b7           name
##  3: 0ae638888fe7f9f1c4e64b417365a1b7          class
##  4: 0ae638888fe7f9f1c4e64b417365a1b7           date
##  5: 0ae638888fe7f9f1c4e64b417365a1b7        cacheId
##  6: 0ae638888fe7f9f1c4e64b417365a1b7     objectName
##  7: 0ae638888fe7f9f1c4e64b417365a1b7       function
##  8: 0ae638888fe7f9f1c4e64b417365a1b7    object.size
##  9: 0ae638888fe7f9f1c4e64b417365a1b7       accessed
## 10: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 11: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 12: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 13: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 14: 0ae638888fe7f9f1c4e64b417365a1b7 otherFunctions
## 15: 0ae638888fe7f9f1c4e64b417365a1b7      preDigest
## 16: 0ae638888fe7f9f1c4e64b417365a1b7      preDigest
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:24
##  2:      0ae638888fe7f9f1c4e64b417365a1b7 2018-07-30 12:15:24
##  3:                               numeric 2018-07-30 12:15:24
##  4:                   2018-07-30 12:15:24 2018-07-30 12:15:24
##  5:      e37bb635c97bc2eeecab63816b881bbc 2018-07-30 12:15:24
##  6:                                     b 2018-07-30 12:15:24
##  7:                                 runif 2018-07-30 12:15:24
##  8:                                   952 2018-07-30 12:15:24
##  9:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 10:                          process_file 2018-07-30 12:15:24
## 11:                         process_group 2018-07-30 12:15:24
## 12:                   process_group.block 2018-07-30 12:15:24
## 13:                            call_block 2018-07-30 12:15:24
## 14:                            block_exec 2018-07-30 12:15:24
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:24
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-07-30 12:15:24
clearCache(tmpDir, userTags = c("runif")) # remove only cached objects made during runif call
## Cache size:
##   Total (including Rasters): 1000 bytes
##   Selected objects (not including Rasters): 238 bytes
showCache(tmpDir) # only those made during rnorm call
## Cache size:
##   Total (including Rasters): 762 bytes
##   Selected objects (not including Rasters): 762 bytes
##                             artifact         tagKey
##  1: 6f53d4a1f0a148000f83cfc8f0a350d4         format
##  2: 6f53d4a1f0a148000f83cfc8f0a350d4           name
##  3: 6f53d4a1f0a148000f83cfc8f0a350d4          class
##  4: 6f53d4a1f0a148000f83cfc8f0a350d4           date
##  5: 6f53d4a1f0a148000f83cfc8f0a350d4        cacheId
##  6: 6f53d4a1f0a148000f83cfc8f0a350d4     objectName
##  7: 6f53d4a1f0a148000f83cfc8f0a350d4       function
##  8: 6f53d4a1f0a148000f83cfc8f0a350d4    object.size
##  9: 6f53d4a1f0a148000f83cfc8f0a350d4       accessed
## 10: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 11: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 12: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 13: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 14: 6f53d4a1f0a148000f83cfc8f0a350d4 otherFunctions
## 15: 6f53d4a1f0a148000f83cfc8f0a350d4      preDigest
## 16: 6f53d4a1f0a148000f83cfc8f0a350d4      preDigest
## 17: 888fcbb0a351b2dc377e47b73b99c282         format
## 18: 888fcbb0a351b2dc377e47b73b99c282           name
## 19: 888fcbb0a351b2dc377e47b73b99c282          class
## 20: 888fcbb0a351b2dc377e47b73b99c282           date
## 21: 888fcbb0a351b2dc377e47b73b99c282        cacheId
## 22: 888fcbb0a351b2dc377e47b73b99c282       function
## 23: 888fcbb0a351b2dc377e47b73b99c282    object.size
## 24: 888fcbb0a351b2dc377e47b73b99c282       accessed
## 25: 888fcbb0a351b2dc377e47b73b99c282 otherFunctions
## 26: 888fcbb0a351b2dc377e47b73b99c282 otherFunctions
## 27: 888fcbb0a351b2dc377e47b73b99c282 otherFunctions
## 28: 888fcbb0a351b2dc377e47b73b99c282 otherFunctions
## 29: 888fcbb0a351b2dc377e47b73b99c282 otherFunctions
## 30: 888fcbb0a351b2dc377e47b73b99c282      preDigest
## 31: 888fcbb0a351b2dc377e47b73b99c282      preDigest
## 32: 888fcbb0a351b2dc377e47b73b99c282      preDigest
## 33: 888fcbb0a351b2dc377e47b73b99c282       accessed
## 34: 888fcbb0a351b2dc377e47b73b99c282       accessed
## 35: 888fcbb0a351b2dc377e47b73b99c282       accessed
## 36: b7334127d401637f2538ec719c716878         format
## 37: b7334127d401637f2538ec719c716878           name
## 38: b7334127d401637f2538ec719c716878          class
## 39: b7334127d401637f2538ec719c716878           date
## 40: b7334127d401637f2538ec719c716878        cacheId
## 41: b7334127d401637f2538ec719c716878       function
## 42: b7334127d401637f2538ec719c716878    object.size
## 43: b7334127d401637f2538ec719c716878       accessed
## 44: b7334127d401637f2538ec719c716878 otherFunctions
## 45: b7334127d401637f2538ec719c716878 otherFunctions
## 46: b7334127d401637f2538ec719c716878 otherFunctions
## 47: b7334127d401637f2538ec719c716878 otherFunctions
## 48: b7334127d401637f2538ec719c716878 otherFunctions
## 49: b7334127d401637f2538ec719c716878      preDigest
## 50: b7334127d401637f2538ec719c716878      preDigest
## 51: b7334127d401637f2538ec719c716878      preDigest
##                             artifact         tagKey
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:24
##  2:      6f53d4a1f0a148000f83cfc8f0a350d4 2018-07-30 12:15:24
##  3:                               numeric 2018-07-30 12:15:24
##  4:                   2018-07-30 12:15:24 2018-07-30 12:15:24
##  5:      85874f26b2e0c1ef689a7d379d275ebf 2018-07-30 12:15:24
##  6:                                     a 2018-07-30 12:15:24
##  7:                                 rnorm 2018-07-30 12:15:24
##  8:                                   952 2018-07-30 12:15:24
##  9:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 10:                          process_file 2018-07-30 12:15:24
## 11:                         process_group 2018-07-30 12:15:24
## 12:                   process_group.block 2018-07-30 12:15:24
## 13:                            call_block 2018-07-30 12:15:24
## 14:                            block_exec 2018-07-30 12:15:24
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:24
## 16: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:24
## 17:                                   rda 2018-07-30 12:15:24
## 18:      888fcbb0a351b2dc377e47b73b99c282 2018-07-30 12:15:24
## 19:                               numeric 2018-07-30 12:15:24
## 20:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 21:      f185e4f8cea576a595f7b81be17e63b5 2018-07-30 12:15:24
## 22:                                 rnorm 2018-07-30 12:15:24
## 23:                                  1048 2018-07-30 12:15:24
## 24:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 25:                          process_file 2018-07-30 12:15:24
## 26:                         process_group 2018-07-30 12:15:24
## 27:                   process_group.block 2018-07-30 12:15:24
## 28:                            call_block 2018-07-30 12:15:24
## 29:                            block_exec 2018-07-30 12:15:24
## 30:    n:52ad08b1270ae9be9c0a12805d408433 2018-07-30 12:15:24
## 31: mean:2c67b8e97ab3ea8d032fea4318223887 2018-07-30 12:15:24
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:24
## 33:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 34:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 35:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 36:                                   rda 2018-07-30 12:15:24
## 37:      b7334127d401637f2538ec719c716878 2018-07-30 12:15:24
## 38:                               numeric 2018-07-30 12:15:24
## 39:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 40:      778e2e6b9a967175d1f08811a995269d 2018-07-30 12:15:24
## 41:                   rnorm pipe sequence 2018-07-30 12:15:24
## 42:                                  1048 2018-07-30 12:15:24
## 43:                   2018-07-30 12:15:24 2018-07-30 12:15:24
## 44:                          process_file 2018-07-30 12:15:24
## 45:                         process_group 2018-07-30 12:15:24
## 46:                   process_group.block 2018-07-30 12:15:24
## 47:                            call_block 2018-07-30 12:15:24
## 48:                            block_exec 2018-07-30 12:15:24
## 49:    n:52ad08b1270ae9be9c0a12805d408433 2018-07-30 12:15:24
## 50: mean:6f529546a7c8cf6edda6081007b3d58d 2018-07-30 12:15:24
## 51: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:24
##                                  tagValue         createdDate
clearCache(tmpDir)
## Cache size:
##   Total (including Rasters): 762 bytes
##   Selected objects (not including Rasters): 762 bytes

1.5 Example 2: using the “accessed” tag

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

# access it again, from Cache
ranNumsA <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:a")
##   loading cached result from previous rnorm call, adding to memoised copy
wholeCache <- showCache(tmpDir)
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 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): 476 bytes
##   Selected objects (not including Rasters): 476 bytes
# inverse join with 2 data.tables ... using: a[!b]
# i.e., return all of wholeCache that was not recently accessed
toRemove <- unique(wholeCache[!onlyRecentlyAccessed], by = "artifact")$artifact
clearCache(tmpDir, toRemove) # remove ones not recently accessed
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 bytes
showCache(tmpDir) # still has more recently accessed
## Cache size:
##   Total (including Rasters): 0 bytes
##   Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows) of 3 cols: md5hash,name,createdDate
clearCache(tmpDir)
## Cache size:
##   Total (including Rasters): 0 bytes
##   Selected objects (not including Rasters): 0 bytes

1.6 Example 3: using keepCache

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

# keep only those cached items from the last 24 hours
oneDay <- 60 * 60 * 24
keepCache(tmpDir, after = Sys.time() - oneDay)
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 bytes
##                             artifact         tagKey
##  1: 3dc1587ef98cb40184fa4fb50ae765fc         format
##  2: 3dc1587ef98cb40184fa4fb50ae765fc           name
##  3: 3dc1587ef98cb40184fa4fb50ae765fc          class
##  4: 3dc1587ef98cb40184fa4fb50ae765fc           date
##  5: 3dc1587ef98cb40184fa4fb50ae765fc        cacheId
##  6: 3dc1587ef98cb40184fa4fb50ae765fc     objectName
##  7: 3dc1587ef98cb40184fa4fb50ae765fc       function
##  8: 3dc1587ef98cb40184fa4fb50ae765fc    object.size
##  9: 3dc1587ef98cb40184fa4fb50ae765fc       accessed
## 10: 3dc1587ef98cb40184fa4fb50ae765fc otherFunctions
## 11: 3dc1587ef98cb40184fa4fb50ae765fc otherFunctions
## 12: 3dc1587ef98cb40184fa4fb50ae765fc otherFunctions
## 13: 3dc1587ef98cb40184fa4fb50ae765fc otherFunctions
## 14: 3dc1587ef98cb40184fa4fb50ae765fc otherFunctions
## 15: 3dc1587ef98cb40184fa4fb50ae765fc      preDigest
## 16: 3dc1587ef98cb40184fa4fb50ae765fc      preDigest
## 17: 968100f99a002b7bc124516e2e02923b         format
## 18: 968100f99a002b7bc124516e2e02923b           name
## 19: 968100f99a002b7bc124516e2e02923b          class
## 20: 968100f99a002b7bc124516e2e02923b           date
## 21: 968100f99a002b7bc124516e2e02923b        cacheId
## 22: 968100f99a002b7bc124516e2e02923b     objectName
## 23: 968100f99a002b7bc124516e2e02923b       function
## 24: 968100f99a002b7bc124516e2e02923b    object.size
## 25: 968100f99a002b7bc124516e2e02923b       accessed
## 26: 968100f99a002b7bc124516e2e02923b otherFunctions
## 27: 968100f99a002b7bc124516e2e02923b otherFunctions
## 28: 968100f99a002b7bc124516e2e02923b otherFunctions
## 29: 968100f99a002b7bc124516e2e02923b otherFunctions
## 30: 968100f99a002b7bc124516e2e02923b otherFunctions
## 31: 968100f99a002b7bc124516e2e02923b      preDigest
## 32: 968100f99a002b7bc124516e2e02923b      preDigest
##                             artifact         tagKey
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:25
##  2:      3dc1587ef98cb40184fa4fb50ae765fc 2018-07-30 12:15:25
##  3:                               numeric 2018-07-30 12:15:25
##  4:                   2018-07-30 12:15:25 2018-07-30 12:15:25
##  5:      e37bb635c97bc2eeecab63816b881bbc 2018-07-30 12:15:25
##  6:                                     b 2018-07-30 12:15:25
##  7:                                 runif 2018-07-30 12:15:25
##  8:                                   952 2018-07-30 12:15:25
##  9:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 10:                          process_file 2018-07-30 12:15:25
## 11:                         process_group 2018-07-30 12:15:25
## 12:                   process_group.block 2018-07-30 12:15:25
## 13:                            call_block 2018-07-30 12:15:25
## 14:                            block_exec 2018-07-30 12:15:25
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-07-30 12:15:25
## 17:                                   rda 2018-07-30 12:15:25
## 18:      968100f99a002b7bc124516e2e02923b 2018-07-30 12:15:25
## 19:                               numeric 2018-07-30 12:15:25
## 20:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 21:      85874f26b2e0c1ef689a7d379d275ebf 2018-07-30 12:15:25
## 22:                                     a 2018-07-30 12:15:25
## 23:                                 rnorm 2018-07-30 12:15:25
## 24:                                   952 2018-07-30 12:15:25
## 25:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 26:                          process_file 2018-07-30 12:15:25
## 27:                         process_group 2018-07-30 12:15:25
## 28:                   process_group.block 2018-07-30 12:15:25
## 29:                            call_block 2018-07-30 12:15:25
## 30:                            block_exec 2018-07-30 12:15:25
## 31:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:25
##                                  tagValue         createdDate
# Keep all Cache items created with an rnorm() call
keepCache(tmpDir, userTags = "rnorm")
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 238 bytes
##                             artifact         tagKey
##  1: 968100f99a002b7bc124516e2e02923b         format
##  2: 968100f99a002b7bc124516e2e02923b           name
##  3: 968100f99a002b7bc124516e2e02923b          class
##  4: 968100f99a002b7bc124516e2e02923b           date
##  5: 968100f99a002b7bc124516e2e02923b        cacheId
##  6: 968100f99a002b7bc124516e2e02923b     objectName
##  7: 968100f99a002b7bc124516e2e02923b       function
##  8: 968100f99a002b7bc124516e2e02923b    object.size
##  9: 968100f99a002b7bc124516e2e02923b       accessed
## 10: 968100f99a002b7bc124516e2e02923b otherFunctions
## 11: 968100f99a002b7bc124516e2e02923b otherFunctions
## 12: 968100f99a002b7bc124516e2e02923b otherFunctions
## 13: 968100f99a002b7bc124516e2e02923b otherFunctions
## 14: 968100f99a002b7bc124516e2e02923b otherFunctions
## 15: 968100f99a002b7bc124516e2e02923b      preDigest
## 16: 968100f99a002b7bc124516e2e02923b      preDigest
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:25
##  2:      968100f99a002b7bc124516e2e02923b 2018-07-30 12:15:25
##  3:                               numeric 2018-07-30 12:15:25
##  4:                   2018-07-30 12:15:25 2018-07-30 12:15:25
##  5:      85874f26b2e0c1ef689a7d379d275ebf 2018-07-30 12:15:25
##  6:                                     a 2018-07-30 12:15:25
##  7:                                 rnorm 2018-07-30 12:15:25
##  8:                                   952 2018-07-30 12:15:25
##  9:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 10:                          process_file 2018-07-30 12:15:25
## 11:                         process_group 2018-07-30 12:15:25
## 12:                   process_group.block 2018-07-30 12:15:25
## 13:                            call_block 2018-07-30 12:15:25
## 14:                            block_exec 2018-07-30 12:15:25
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 16: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:25
# Remove all Cache items that happened within a rnorm() call
clearCache(tmpDir, userTags = "rnorm")
## Cache size:
##   Total (including Rasters): 238 bytes
##   Selected objects (not including Rasters): 238 bytes
showCache(tmpDir) ## empty
## Cache size:
##   Total (including Rasters): 0 bytes
##   Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows) of 3 cols: md5hash,name,createdDate

1.7 Example 4: searching for multiple objects in the cache

# default userTags is "and" matching; for "or" matching use |
ranNumsA <- Cache(runif, 4, cacheRepo = tmpDir, userTags = "objectName:a")
ranNumsB <- Cache(rnorm, 4, cacheRepo = tmpDir, userTags = "objectName:b")

# show all objects (runif and rnorm in this case)
showCache(tmpDir)
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 bytes
##                             artifact         tagKey
##  1: 14a6560f258b92c829539d5b50db8087         format
##  2: 14a6560f258b92c829539d5b50db8087           name
##  3: 14a6560f258b92c829539d5b50db8087          class
##  4: 14a6560f258b92c829539d5b50db8087           date
##  5: 14a6560f258b92c829539d5b50db8087        cacheId
##  6: 14a6560f258b92c829539d5b50db8087     objectName
##  7: 14a6560f258b92c829539d5b50db8087       function
##  8: 14a6560f258b92c829539d5b50db8087    object.size
##  9: 14a6560f258b92c829539d5b50db8087       accessed
## 10: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 11: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 12: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 13: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 14: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 15: 14a6560f258b92c829539d5b50db8087      preDigest
## 16: 14a6560f258b92c829539d5b50db8087      preDigest
## 17: 544b53913a84f8886e78825ac4fe7d29         format
## 18: 544b53913a84f8886e78825ac4fe7d29           name
## 19: 544b53913a84f8886e78825ac4fe7d29          class
## 20: 544b53913a84f8886e78825ac4fe7d29           date
## 21: 544b53913a84f8886e78825ac4fe7d29        cacheId
## 22: 544b53913a84f8886e78825ac4fe7d29     objectName
## 23: 544b53913a84f8886e78825ac4fe7d29       function
## 24: 544b53913a84f8886e78825ac4fe7d29    object.size
## 25: 544b53913a84f8886e78825ac4fe7d29       accessed
## 26: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 27: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 28: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 29: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 30: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 31: 544b53913a84f8886e78825ac4fe7d29      preDigest
## 32: 544b53913a84f8886e78825ac4fe7d29      preDigest
##                             artifact         tagKey
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:25
##  2:      14a6560f258b92c829539d5b50db8087 2018-07-30 12:15:25
##  3:                               numeric 2018-07-30 12:15:25
##  4:                   2018-07-30 12:15:25 2018-07-30 12:15:25
##  5:      e37bb635c97bc2eeecab63816b881bbc 2018-07-30 12:15:25
##  6:                                     a 2018-07-30 12:15:25
##  7:                                 runif 2018-07-30 12:15:25
##  8:                                   952 2018-07-30 12:15:25
##  9:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 10:                          process_file 2018-07-30 12:15:25
## 11:                         process_group 2018-07-30 12:15:25
## 12:                   process_group.block 2018-07-30 12:15:25
## 13:                            call_block 2018-07-30 12:15:25
## 14:                            block_exec 2018-07-30 12:15:25
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-07-30 12:15:25
## 17:                                   rda 2018-07-30 12:15:25
## 18:      544b53913a84f8886e78825ac4fe7d29 2018-07-30 12:15:25
## 19:                               numeric 2018-07-30 12:15:25
## 20:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 21:      85874f26b2e0c1ef689a7d379d275ebf 2018-07-30 12:15:25
## 22:                                     b 2018-07-30 12:15:25
## 23:                                 rnorm 2018-07-30 12:15:25
## 24:                                   952 2018-07-30 12:15:25
## 25:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 26:                          process_file 2018-07-30 12:15:25
## 27:                         process_group 2018-07-30 12:15:25
## 28:                   process_group.block 2018-07-30 12:15:25
## 29:                            call_block 2018-07-30 12:15:25
## 30:                            block_exec 2018-07-30 12:15:25
## 31:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:25
##                                  tagValue         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): 476 bytes
##   Selected objects (not including Rasters): 0 bytes
## Empty data.table (0 rows) of 4 cols: artifact,tagKey,tagValue,createdDate
# show objects that are either runif or rnorm ("or" search)
showCache(tmpDir, userTags = "runif|rnorm")
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 bytes
##                             artifact         tagKey
##  1: 14a6560f258b92c829539d5b50db8087         format
##  2: 14a6560f258b92c829539d5b50db8087           name
##  3: 14a6560f258b92c829539d5b50db8087          class
##  4: 14a6560f258b92c829539d5b50db8087           date
##  5: 14a6560f258b92c829539d5b50db8087        cacheId
##  6: 14a6560f258b92c829539d5b50db8087     objectName
##  7: 14a6560f258b92c829539d5b50db8087       function
##  8: 14a6560f258b92c829539d5b50db8087    object.size
##  9: 14a6560f258b92c829539d5b50db8087       accessed
## 10: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 11: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 12: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 13: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 14: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 15: 14a6560f258b92c829539d5b50db8087      preDigest
## 16: 14a6560f258b92c829539d5b50db8087      preDigest
## 17: 544b53913a84f8886e78825ac4fe7d29         format
## 18: 544b53913a84f8886e78825ac4fe7d29           name
## 19: 544b53913a84f8886e78825ac4fe7d29          class
## 20: 544b53913a84f8886e78825ac4fe7d29           date
## 21: 544b53913a84f8886e78825ac4fe7d29        cacheId
## 22: 544b53913a84f8886e78825ac4fe7d29     objectName
## 23: 544b53913a84f8886e78825ac4fe7d29       function
## 24: 544b53913a84f8886e78825ac4fe7d29    object.size
## 25: 544b53913a84f8886e78825ac4fe7d29       accessed
## 26: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 27: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 28: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 29: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 30: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 31: 544b53913a84f8886e78825ac4fe7d29      preDigest
## 32: 544b53913a84f8886e78825ac4fe7d29      preDigest
##                             artifact         tagKey
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:25
##  2:      14a6560f258b92c829539d5b50db8087 2018-07-30 12:15:25
##  3:                               numeric 2018-07-30 12:15:25
##  4:                   2018-07-30 12:15:25 2018-07-30 12:15:25
##  5:      e37bb635c97bc2eeecab63816b881bbc 2018-07-30 12:15:25
##  6:                                     a 2018-07-30 12:15:25
##  7:                                 runif 2018-07-30 12:15:25
##  8:                                   952 2018-07-30 12:15:25
##  9:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 10:                          process_file 2018-07-30 12:15:25
## 11:                         process_group 2018-07-30 12:15:25
## 12:                   process_group.block 2018-07-30 12:15:25
## 13:                            call_block 2018-07-30 12:15:25
## 14:                            block_exec 2018-07-30 12:15:25
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-07-30 12:15:25
## 17:                                   rda 2018-07-30 12:15:25
## 18:      544b53913a84f8886e78825ac4fe7d29 2018-07-30 12:15:25
## 19:                               numeric 2018-07-30 12:15:25
## 20:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 21:      85874f26b2e0c1ef689a7d379d275ebf 2018-07-30 12:15:25
## 22:                                     b 2018-07-30 12:15:25
## 23:                                 rnorm 2018-07-30 12:15:25
## 24:                                   952 2018-07-30 12:15:25
## 25:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 26:                          process_file 2018-07-30 12:15:25
## 27:                         process_group 2018-07-30 12:15:25
## 28:                   process_group.block 2018-07-30 12:15:25
## 29:                            call_block 2018-07-30 12:15:25
## 30:                            block_exec 2018-07-30 12:15:25
## 31:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:25
##                                  tagValue         createdDate
# keep only objects that are either runif or rnorm ("or" search)
keepCache(tmpDir, userTags = "runif|rnorm")
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 bytes
##                             artifact         tagKey
##  1: 14a6560f258b92c829539d5b50db8087         format
##  2: 14a6560f258b92c829539d5b50db8087           name
##  3: 14a6560f258b92c829539d5b50db8087          class
##  4: 14a6560f258b92c829539d5b50db8087           date
##  5: 14a6560f258b92c829539d5b50db8087        cacheId
##  6: 14a6560f258b92c829539d5b50db8087     objectName
##  7: 14a6560f258b92c829539d5b50db8087       function
##  8: 14a6560f258b92c829539d5b50db8087    object.size
##  9: 14a6560f258b92c829539d5b50db8087       accessed
## 10: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 11: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 12: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 13: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 14: 14a6560f258b92c829539d5b50db8087 otherFunctions
## 15: 14a6560f258b92c829539d5b50db8087      preDigest
## 16: 14a6560f258b92c829539d5b50db8087      preDigest
## 17: 544b53913a84f8886e78825ac4fe7d29         format
## 18: 544b53913a84f8886e78825ac4fe7d29           name
## 19: 544b53913a84f8886e78825ac4fe7d29          class
## 20: 544b53913a84f8886e78825ac4fe7d29           date
## 21: 544b53913a84f8886e78825ac4fe7d29        cacheId
## 22: 544b53913a84f8886e78825ac4fe7d29     objectName
## 23: 544b53913a84f8886e78825ac4fe7d29       function
## 24: 544b53913a84f8886e78825ac4fe7d29    object.size
## 25: 544b53913a84f8886e78825ac4fe7d29       accessed
## 26: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 27: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 28: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 29: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 30: 544b53913a84f8886e78825ac4fe7d29 otherFunctions
## 31: 544b53913a84f8886e78825ac4fe7d29      preDigest
## 32: 544b53913a84f8886e78825ac4fe7d29      preDigest
##                             artifact         tagKey
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:25
##  2:      14a6560f258b92c829539d5b50db8087 2018-07-30 12:15:25
##  3:                               numeric 2018-07-30 12:15:25
##  4:                   2018-07-30 12:15:25 2018-07-30 12:15:25
##  5:      e37bb635c97bc2eeecab63816b881bbc 2018-07-30 12:15:25
##  6:                                     a 2018-07-30 12:15:25
##  7:                                 runif 2018-07-30 12:15:25
##  8:                                   952 2018-07-30 12:15:25
##  9:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 10:                          process_file 2018-07-30 12:15:25
## 11:                         process_group 2018-07-30 12:15:25
## 12:                   process_group.block 2018-07-30 12:15:25
## 13:                            call_block 2018-07-30 12:15:25
## 14:                            block_exec 2018-07-30 12:15:25
## 15:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 16: .FUN:d2631d24c3b38b89c7bdd4ab7faaaac3 2018-07-30 12:15:25
## 17:                                   rda 2018-07-30 12:15:25
## 18:      544b53913a84f8886e78825ac4fe7d29 2018-07-30 12:15:25
## 19:                               numeric 2018-07-30 12:15:25
## 20:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 21:      85874f26b2e0c1ef689a7d379d275ebf 2018-07-30 12:15:25
## 22:                                     b 2018-07-30 12:15:25
## 23:                                 rnorm 2018-07-30 12:15:25
## 24:                                   952 2018-07-30 12:15:25
## 25:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 26:                          process_file 2018-07-30 12:15:25
## 27:                         process_group 2018-07-30 12:15:25
## 28:                   process_group.block 2018-07-30 12:15:25
## 29:                            call_block 2018-07-30 12:15:25
## 30:                            block_exec 2018-07-30 12:15:25
## 31:    n:969a49ec15bcd4323ff31538af321264 2018-07-30 12:15:25
## 32: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:25
##                                  tagValue         createdDate
clearCache(tmpDir)
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 bytes

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

ras <- raster(extent(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")

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

cached <- suppressWarnings(
  # project raster generates warnings when run non-interactively
  # using quote works also
  Cache(projectRaster, ras, crs = crs(ras), res = 5, cacheRepo = tmpDir)
)

# second time is much faster
reRun <- suppressWarnings(
  # project raster generates warnings when run non-interactively
  Cache(projectRaster, ras, crs = crs(ras), res = 5, cacheRepo = tmpDir)
)
##   loading cached result from previous projectRaster call, adding to memoised copy
# recovered cached version is same as non-cached version
all.equal(notCached, reRun) ## TRUE
## [1] TRUE

1.9 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 that 1 second. 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, cacheRepo = 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 cacheRepo is tmpdir1 in top level Cache and all nested
#   Cache calls, unless individually overridden ... here inner
#   uses tmpdir2 repository
Cache(outer, n = 2, cacheRepo = tmpdir1, notOlderThan = Sys.time())
## [1] -0.9718296  1.1554114  0.6943953
## attr(,"tags")
## [1] "cacheId:e09bf93970f9e94d5c639cfa8ca722f0"
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"call")
## [1] ""
showCache(tmpdir1) # 2 function calls
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 bytes
##                             artifact         tagKey
##  1: 980a1e425d06deb334b27586fadcbc37         format
##  2: 980a1e425d06deb334b27586fadcbc37           name
##  3: 980a1e425d06deb334b27586fadcbc37          class
##  4: 980a1e425d06deb334b27586fadcbc37           date
##  5: 980a1e425d06deb334b27586fadcbc37        cacheId
##  6: 980a1e425d06deb334b27586fadcbc37       function
##  7: 980a1e425d06deb334b27586fadcbc37    object.size
##  8: 980a1e425d06deb334b27586fadcbc37       accessed
##  9: 980a1e425d06deb334b27586fadcbc37 otherFunctions
## 10: 980a1e425d06deb334b27586fadcbc37 otherFunctions
## 11: 980a1e425d06deb334b27586fadcbc37 otherFunctions
## 12: 980a1e425d06deb334b27586fadcbc37 otherFunctions
## 13: 980a1e425d06deb334b27586fadcbc37 otherFunctions
## 14: 980a1e425d06deb334b27586fadcbc37      preDigest
## 15: 980a1e425d06deb334b27586fadcbc37      preDigest
## 16: b3ae0900baab3c898f10c5c570208e31         format
## 17: b3ae0900baab3c898f10c5c570208e31           name
## 18: b3ae0900baab3c898f10c5c570208e31          class
## 19: b3ae0900baab3c898f10c5c570208e31           date
## 20: b3ae0900baab3c898f10c5c570208e31        cacheId
## 21: b3ae0900baab3c898f10c5c570208e31       function
## 22: b3ae0900baab3c898f10c5c570208e31    object.size
## 23: b3ae0900baab3c898f10c5c570208e31       accessed
## 24: b3ae0900baab3c898f10c5c570208e31 otherFunctions
## 25: b3ae0900baab3c898f10c5c570208e31 otherFunctions
## 26: b3ae0900baab3c898f10c5c570208e31 otherFunctions
## 27: b3ae0900baab3c898f10c5c570208e31 otherFunctions
## 28: b3ae0900baab3c898f10c5c570208e31 otherFunctions
## 29: b3ae0900baab3c898f10c5c570208e31      preDigest
## 30: b3ae0900baab3c898f10c5c570208e31      preDigest
## 31: b3ae0900baab3c898f10c5c570208e31      preDigest
##                             artifact         tagKey
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:25
##  2:      980a1e425d06deb334b27586fadcbc37 2018-07-30 12:15:25
##  3:                               numeric 2018-07-30 12:15:26
##  4:                   2018-07-30 12:15:25 2018-07-30 12:15:26
##  5:      e09bf93970f9e94d5c639cfa8ca722f0 2018-07-30 12:15:26
##  6:                                 outer 2018-07-30 12:15:26
##  7:                                   952 2018-07-30 12:15:26
##  8:                   2018-07-30 12:15:25 2018-07-30 12:15:26
##  9:                          process_file 2018-07-30 12:15:26
## 10:                         process_group 2018-07-30 12:15:26
## 11:                   process_group.block 2018-07-30 12:15:26
## 12:                            call_block 2018-07-30 12:15:26
## 13:                            block_exec 2018-07-30 12:15:26
## 14:    n:8128a6180a705341ab7c05cfa945edfb 2018-07-30 12:15:26
## 15: .FUN:b5f6bcbdd9f23e39c2c5d4020e73a6ff 2018-07-30 12:15:26
## 16:                                   rda 2018-07-30 12:15:25
## 17:      b3ae0900baab3c898f10c5c570208e31 2018-07-30 12:15:25
## 18:                               numeric 2018-07-30 12:15:25
## 19:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 20:      cec73d63ad3864af8bcd7efc5fae864d 2018-07-30 12:15:25
## 21:                                 rnorm 2018-07-30 12:15:25
## 22:                                   952 2018-07-30 12:15:25
## 23:                   2018-07-30 12:15:25 2018-07-30 12:15:25
## 24:                          process_file 2018-07-30 12:15:25
## 25:                         process_group 2018-07-30 12:15:25
## 26:                   process_group.block 2018-07-30 12:15:25
## 27:                            call_block 2018-07-30 12:15:25
## 28:                            block_exec 2018-07-30 12:15:25
## 29:    n:4ae3e6b6364de42fdc243469d73448cc 2018-07-30 12:15:25
## 30: mean:c28b87a0be6a99966bdaa5e556974b43 2018-07-30 12:15:25
## 31: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:25
##                                  tagValue         createdDate
showCache(tmpdir2) # 1 function call
## Cache size:
##   Total (including Rasters): 238 bytes
##   Selected objects (not including Rasters): 238 bytes
##                             artifact         tagKey
##  1: 21946572e4717102c070105bf19696e8         format
##  2: 21946572e4717102c070105bf19696e8           name
##  3: 21946572e4717102c070105bf19696e8          class
##  4: 21946572e4717102c070105bf19696e8           date
##  5: 21946572e4717102c070105bf19696e8        cacheId
##  6: 21946572e4717102c070105bf19696e8       function
##  7: 21946572e4717102c070105bf19696e8    object.size
##  8: 21946572e4717102c070105bf19696e8       accessed
##  9: 21946572e4717102c070105bf19696e8 otherFunctions
## 10: 21946572e4717102c070105bf19696e8 otherFunctions
## 11: 21946572e4717102c070105bf19696e8 otherFunctions
## 12: 21946572e4717102c070105bf19696e8 otherFunctions
## 13: 21946572e4717102c070105bf19696e8 otherFunctions
## 14: 21946572e4717102c070105bf19696e8      preDigest
## 15: 21946572e4717102c070105bf19696e8      preDigest
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:25
##  2:      21946572e4717102c070105bf19696e8 2018-07-30 12:15:25
##  3:                               numeric 2018-07-30 12:15:25
##  4:                   2018-07-30 12:15:25 2018-07-30 12:15:25
##  5:      19b808ac6871e0184e63c421a116cb61 2018-07-30 12:15:25
##  6:                                 inner 2018-07-30 12:15:25
##  7:                                   952 2018-07-30 12:15:25
##  8:                   2018-07-30 12:15:25 2018-07-30 12:15:25
##  9:                          process_file 2018-07-30 12:15:25
## 10:                         process_group 2018-07-30 12:15:25
## 11:                   process_group.block 2018-07-30 12:15:25
## 12:                            call_block 2018-07-30 12:15:25
## 13:                            block_exec 2018-07-30 12:15:25
## 14: mean:c28b87a0be6a99966bdaa5e556974b43 2018-07-30 12:15:25
## 15: .FUN:56a1302d7ef43383766d7af6ca072c4e 2018-07-30 12:15:25
# userTags get appended
# all items have the outer tag propagate, plus inner ones only have inner ones
clearCache(tmpdir1)
## Cache size:
##   Total (including Rasters): 476 bytes
##   Selected objects (not including Rasters): 476 bytes
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, cacheRepo = tmpdir1, userTags = outerTag)
showCache(tmpdir1) # rnorm function has outerTag and innerTag, inner and outer only have outerTag
## Cache size:
##   Total (including Rasters): 714 bytes
##   Selected objects (not including Rasters): 714 bytes
##                             artifact         tagKey
##  1: 219416470392848e85f471cf81d30b5f         format
##  2: 219416470392848e85f471cf81d30b5f           name
##  3: 219416470392848e85f471cf81d30b5f          class
##  4: 219416470392848e85f471cf81d30b5f           date
##  5: 219416470392848e85f471cf81d30b5f        cacheId
##  6: 219416470392848e85f471cf81d30b5f       outerTag
##  7: 219416470392848e85f471cf81d30b5f       function
##  8: 219416470392848e85f471cf81d30b5f    object.size
##  9: 219416470392848e85f471cf81d30b5f       accessed
## 10: 219416470392848e85f471cf81d30b5f otherFunctions
## 11: 219416470392848e85f471cf81d30b5f otherFunctions
## 12: 219416470392848e85f471cf81d30b5f otherFunctions
## 13: 219416470392848e85f471cf81d30b5f otherFunctions
## 14: 219416470392848e85f471cf81d30b5f otherFunctions
## 15: 219416470392848e85f471cf81d30b5f      preDigest
## 16: 219416470392848e85f471cf81d30b5f      preDigest
## 17: 9ed7c506505147ea9ce2b0b26209c8ad         format
## 18: 9ed7c506505147ea9ce2b0b26209c8ad           name
## 19: 9ed7c506505147ea9ce2b0b26209c8ad          class
## 20: 9ed7c506505147ea9ce2b0b26209c8ad           date
## 21: 9ed7c506505147ea9ce2b0b26209c8ad        cacheId
## 22: 9ed7c506505147ea9ce2b0b26209c8ad       outerTag
## 23: 9ed7c506505147ea9ce2b0b26209c8ad       function
## 24: 9ed7c506505147ea9ce2b0b26209c8ad    object.size
## 25: 9ed7c506505147ea9ce2b0b26209c8ad       accessed
## 26: 9ed7c506505147ea9ce2b0b26209c8ad otherFunctions
## 27: 9ed7c506505147ea9ce2b0b26209c8ad otherFunctions
## 28: 9ed7c506505147ea9ce2b0b26209c8ad otherFunctions
## 29: 9ed7c506505147ea9ce2b0b26209c8ad otherFunctions
## 30: 9ed7c506505147ea9ce2b0b26209c8ad otherFunctions
## 31: 9ed7c506505147ea9ce2b0b26209c8ad      preDigest
## 32: 9ed7c506505147ea9ce2b0b26209c8ad      preDigest
## 33: e0927d88026bae8ed0446536cb89e97b         format
## 34: e0927d88026bae8ed0446536cb89e97b           name
## 35: e0927d88026bae8ed0446536cb89e97b          class
## 36: e0927d88026bae8ed0446536cb89e97b           date
## 37: e0927d88026bae8ed0446536cb89e97b        cacheId
## 38: e0927d88026bae8ed0446536cb89e97b       innerTag
## 39: e0927d88026bae8ed0446536cb89e97b       outerTag
## 40: e0927d88026bae8ed0446536cb89e97b       function
## 41: e0927d88026bae8ed0446536cb89e97b    object.size
## 42: e0927d88026bae8ed0446536cb89e97b       accessed
## 43: e0927d88026bae8ed0446536cb89e97b otherFunctions
## 44: e0927d88026bae8ed0446536cb89e97b otherFunctions
## 45: e0927d88026bae8ed0446536cb89e97b otherFunctions
## 46: e0927d88026bae8ed0446536cb89e97b otherFunctions
## 47: e0927d88026bae8ed0446536cb89e97b otherFunctions
## 48: e0927d88026bae8ed0446536cb89e97b      preDigest
## 49: e0927d88026bae8ed0446536cb89e97b      preDigest
## 50: e0927d88026bae8ed0446536cb89e97b      preDigest
##                             artifact         tagKey
##                                  tagValue         createdDate
##  1:                                   rda 2018-07-30 12:15:26
##  2:      219416470392848e85f471cf81d30b5f 2018-07-30 12:15:26
##  3:                               numeric 2018-07-30 12:15:26
##  4:                   2018-07-30 12:15:26 2018-07-30 12:15:26
##  5:      44f57deb36c53cd9c395e04c51fea77a 2018-07-30 12:15:26
##  6:                              outerTag 2018-07-30 12:15:26
##  7:                                 outer 2018-07-30 12:15:26
##  8:                                   952 2018-07-30 12:15:26
##  9:                   2018-07-30 12:15:26 2018-07-30 12:15:26
## 10:                          process_file 2018-07-30 12:15:26
## 11:                         process_group 2018-07-30 12:15:26
## 12:                   process_group.block 2018-07-30 12:15:26
## 13:                            call_block 2018-07-30 12:15:26
## 14:                            block_exec 2018-07-30 12:15:26
## 15:    n:8128a6180a705341ab7c05cfa945edfb 2018-07-30 12:15:26
## 16: .FUN:62302feda89e19149a56ca40fde725e1 2018-07-30 12:15:26
## 17:                                   rda 2018-07-30 12:15:26
## 18:      9ed7c506505147ea9ce2b0b26209c8ad 2018-07-30 12:15:26
## 19:                               numeric 2018-07-30 12:15:26
## 20:                   2018-07-30 12:15:26 2018-07-30 12:15:26
## 21:      994d1330fbd961f795ab0dc508271963 2018-07-30 12:15:26
## 22:                              outerTag 2018-07-30 12:15:26
## 23:                                 inner 2018-07-30 12:15:26
## 24:                                   952 2018-07-30 12:15:26
## 25:                   2018-07-30 12:15:26 2018-07-30 12:15:26
## 26:                          process_file 2018-07-30 12:15:26
## 27:                         process_group 2018-07-30 12:15:26
## 28:                   process_group.block 2018-07-30 12:15:26
## 29:                            call_block 2018-07-30 12:15:26
## 30:                            block_exec 2018-07-30 12:15:26
## 31: mean:c28b87a0be6a99966bdaa5e556974b43 2018-07-30 12:15:26
## 32: .FUN:b910401646b09073940de757678db03d 2018-07-30 12:15:26
## 33:                                   rda 2018-07-30 12:15:26
## 34:      e0927d88026bae8ed0446536cb89e97b 2018-07-30 12:15:26
## 35:                               numeric 2018-07-30 12:15:26
## 36:                   2018-07-30 12:15:26 2018-07-30 12:15:26
## 37:      cec73d63ad3864af8bcd7efc5fae864d 2018-07-30 12:15:26
## 38:                              innerTag 2018-07-30 12:15:26
## 39:                              outerTag 2018-07-30 12:15:26
## 40:                                 rnorm 2018-07-30 12:15:26
## 41:                                   952 2018-07-30 12:15:26
## 42:                   2018-07-30 12:15:26 2018-07-30 12:15:26
## 43:                          process_file 2018-07-30 12:15:26
## 44:                         process_group 2018-07-30 12:15:26
## 45:                   process_group.block 2018-07-30 12:15:26
## 46:                            call_block 2018-07-30 12:15:26
## 47:                            block_exec 2018-07-30 12:15:26
## 48:    n:4ae3e6b6364de42fdc243469d73448cc 2018-07-30 12:15:26
## 49: mean:c28b87a0be6a99966bdaa5e556974b43 2018-07-30 12:15:26
## 50: .FUN:7e9a928f110f80b3612e71883a6ec1f4 2018-07-30 12:15:26
##                                  tagValue         createdDate

1.10 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, cacheRepo = tmpdir1)
## [1] -0.6264538
## attr(,"tags")
## [1] "cacheId:23dc247384c1b270f0d36de4bca1b138"
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"call")
## [1] ""
# manually look at output attribute which shows cacheId: ad184ce64541972b50afd8e7b75f821b
Cache(rnorm, 1, cacheRepo = tmpdir1, cacheId = "ad184ce64541972b50afd8e7b75f821b") # same value
## cacheId is not same as calculated hash. Manually searching for cacheId:ad184ce64541972b50afd8e7b75f821b
## [1] 0.1836433
## attr(,"tags")
## [1] "cacheId:ad184ce64541972b50afd8e7b75f821b"
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] TRUE
## 
## attr(,"call")
## [1] ""
# override even with different inputs:
Cache(rnorm, 2, cacheRepo = tmpdir1, cacheId = "ad184ce64541972b50afd8e7b75f821b")
## cacheId is not same as calculated hash. Manually searching for cacheId:ad184ce64541972b50afd8e7b75f821b
##   loading cached result from previous rnorm call, adding to memoised copy
## [1] 0.1836433
## attr(,"tags")
## [1] "cacheId:ad184ce64541972b50afd8e7b75f821b"
## attr(,".Cache")
## attr(,".Cache")$newCache
## [1] FALSE
## 
## attr(,"call")
## [1] ""
## cleanup
unlink(c("filename.rda", "filename1.rda"))

1.11 Working with the Cache manually

Since the cache is simply an archivist repository, all archivist functions will work as is. 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).

if (requireNamespace("archivist")) {
  # get the RasterLayer that was produced with the gaussMap function:
  mapHash <- unique(showCache(tmpDir, userTags = "projectRaster")$artifact)
  map <- archivist::loadFromLocalRepo(md5hash = mapHash[1], repoDir = tmpDir, value = TRUE)
  
  plot(map)
}
## Cache size:
##   Total (including Rasters): 3.1 Kb
##   Selected objects (not including Rasters): 3.1 Kb

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

2 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.

2.1 Nested Caching

2.1.0.1 Cache individual functions

Cache(<functionName>, <other arguments>)

This will allow fine scale control of individual function calls.