-
-
Notifications
You must be signed in to change notification settings - Fork 878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
customizable cache (closes #2176) #2340
Open
atusy
wants to merge
4
commits into
yihui:master
Choose a base branch
from
atusy:cache-hook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f2d52f7
refactor(cache): use saveRDS/readRDS instead of makeLazyLoadDB/lazyload
atusy 94cca33
feat(cache): allow pre/postprocessing cache objects
atusy 4dddaba
feat!(cache): implement knit_cache_hook instead of pre/post-processors
atusy 78435c3
chore(docs): build docs
atusy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ new_cache = function() { | |
} | ||
|
||
cache_purge = function(hash) { | ||
for (h in hash) unlink(paste(cache_path(h), c('rdb', 'rdx', 'RData'), sep = '.')) | ||
for (h in hash) unlink(paste(cache_path(h), c('rds', 'rdb', 'rdx', 'RData'), sep = '.')) | ||
} | ||
|
||
cache_save = function(keys, outname, hash, lazy = TRUE) { | ||
|
@@ -31,7 +31,9 @@ new_cache = function() { | |
if (!lazy) return() # everything has been saved; no need to make lazy db | ||
# random seed is always load()ed | ||
keys = as.character(setdiff(keys, '.Random.seed')) | ||
getFromNamespace('makeLazyLoadDB', 'tools')(knit_global(), path, variables = keys) | ||
envir = knit_global() | ||
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 | ||
} | ||
|
||
save_objects = function(objs, label, path) { | ||
|
@@ -56,7 +58,17 @@ new_cache = function() { | |
cache_load = function(hash, lazy = TRUE) { | ||
path = cache_path(hash) | ||
if (!is_abs_path(path)) path = file.path(getwd(), path) | ||
if (lazy) lazyLoad(path, envir = knit_global()) | ||
if (lazy) { | ||
if (file.exists(paste(path, 'rdb', sep = '.'))) { | ||
lazyLoad(path, envir = knit_global()) # backward compatibility | ||
} else { | ||
envir = knit_global() | ||
obj = readRDS(paste(path, 'rds', sep = '.')) | ||
for (nm in names(obj)) { | ||
assign(nm, knit_cache_postprocess(obj[[nm]]), envir = envir) | ||
} | ||
} | ||
} | ||
# load output from last run if exists | ||
if (file.exists(path2 <- paste(path, 'RData', sep = '.'))) { | ||
load(path2, envir = knit_global()) | ||
|
@@ -87,10 +99,12 @@ new_cache = function() { | |
} | ||
|
||
cache_exists = function(hash, lazy = TRUE) { | ||
is.character(hash) && | ||
all(file.exists(paste( | ||
cache_path(hash), if (lazy) c('rdb', 'rdx') else 'RData', sep = '.' | ||
))) | ||
if (!is.character(hash)) return(FALSE) | ||
path = cache_path(hash) | ||
if (!lazy) return(file.exists(paste(path, 'RData', sep = '.'))) | ||
|
||
# for backward compatibility, allow rdb/rdx | ||
file.exists(paste(path, 'rds', sep = '.')) || all(file.exists(paste(path, c('rdb', 'rdx'), sep = '.'))) | ||
} | ||
|
||
# when cache=3, code output is stored in .[hash], so cache=TRUE won't lose | ||
|
@@ -128,10 +142,16 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Postprocess is skipped if a package is not loaded. |
||
|
||
cache = new_cache() | ||
|
||
# a regex for cache files | ||
cache_rx = '_[abcdef0123456789]{32}[.](rdb|rdx|RData)$' | ||
cache_rx = '_[abcdef0123456789]{32}[.](rds|rdb|rdx|RData)$' | ||
|
||
#' Build automatic dependencies among chunks | ||
#' | ||
|
@@ -246,7 +266,7 @@ load_cache = function( | |
'Wrong cache databases for the chunk ', label, | ||
'. You need to remove redundant cache files. Found ', paste(p2, collapse = ', ') | ||
) | ||
p2 = unique(gsub('[.](rdb|rdx|RData)$', '', p2)) | ||
p2 = unique(gsub('[.](rds|rdb|rdx|RData)$', '', p2)) | ||
if (length(p2) != 1) stop('Cannot identify the cache database for chunk ', label) | ||
cache$load(file.path(p0, p2), lazy) | ||
if (missing(object)) return(invisible(NULL)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cache_purge()
andclean_cache()
take into account of limited file types/names.There may be some cases where knitr should remove more files.
The example in the description saved an extra file as a part of cache, which will not be removed by knitr.