Skip to content

Commit 94cca33

Browse files
committed
feat(cache): allow pre/postprocessing cache objects
1 parent f2d52f7 commit 94cca33

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

R/cache.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ new_cache = function() {
3232
# random seed is always load()ed
3333
keys = as.character(setdiff(keys, '.Random.seed'))
3434
envir = knit_global()
35-
saveRDS(setNames(lapply(keys, function(k) envir[[k]]), keys), paste(path, 'rds', sep = '.'))
35+
saveRDS(setNames(lapply(keys, function(k) knit_cache_preprocess(envir[[k]])), keys), paste(path, 'rds', sep = '.'))
3636
unlink(paste(path, c('rdb', 'rdx'), sep = '.')) # migrate from former implementation
3737
}
3838

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

145+
# process cached objects before save and after read
146+
knit_cache_preprocess = function(x, ...) UseMethod('knit_cache_preprocess')
147+
knit_cache_preprocess.default = function(x, ...) x
148+
knit_cache_postprocess = function(x, ...) UseMethod('knit_cache_postprocess')
149+
knit_cache_postprocess.default = function(x, ...) x
150+
145151
cache = new_cache()
146152

147153
# a regex for cache files

0 commit comments

Comments
 (0)