Skip to content

Commit

Permalink
feat(cache): allow pre/postprocessing cache objects
Browse files Browse the repository at this point in the history
  • Loading branch information
atusy committed Apr 29, 2024
1 parent f2d52f7 commit 94cca33
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ new_cache = function() {
# random seed is always load()ed
keys = as.character(setdiff(keys, '.Random.seed'))
envir = knit_global()
saveRDS(setNames(lapply(keys, function(k) envir[[k]]), keys), paste(path, 'rds', sep = '.'))
saveRDS(setNames(lapply(keys, function(k) knit_cache_preprocess(envir[[k]])), keys), paste(path, 'rds', sep = '.'))
unlink(paste(path, c('rdb', 'rdx'), sep = '.')) # migrate from former implementation
}

Expand Down Expand Up @@ -65,7 +65,7 @@ new_cache = function() {
envir = knit_global()
obj = readRDS(paste(path, 'rds', sep = '.'))
for (nm in names(obj)) {
assign(nm, obj[[nm]], envir = envir)
assign(nm, knit_cache_postprocess(obj[[nm]]), envir = envir)
}
}
}
Expand Down Expand Up @@ -142,6 +142,12 @@ cache_meta_name = function(hash) sprintf('.%s_meta', hash)
# a variable name to store the text output of code chunks
cache_output_name = function(hash) sprintf('.%s', hash)

# process cached objects before save and after read
knit_cache_preprocess = function(x, ...) UseMethod('knit_cache_preprocess')
knit_cache_preprocess.default = function(x, ...) x
knit_cache_postprocess = function(x, ...) UseMethod('knit_cache_postprocess')
knit_cache_postprocess.default = function(x, ...) x

cache = new_cache()

# a regex for cache files
Expand Down

0 comments on commit 94cca33

Please sign in to comment.