Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(print,memoised)
export(cache)
export(cache_filesystem)
export(cache_memory)
export(cache_s3)
Expand Down
18 changes: 18 additions & 0 deletions R/cache.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' @export
cache <- function(code, ..., envir = parent.frame(), cache = cache_memory()) {
expr <- substitute(code)
key <- cache$digest(c(expr, lapply(list(...), function(x) eval(x[[2L]], environment(x)))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the eval necessary here? Won't list(...) force all the promises?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh because they're formulas

if (cache$has_key(key)) {
res <- cache$get(key)
if (res$visible) {
res$value
} else {
invisible(res$value)
}
} else {
f <- function() NULL
body(f) <- expr
environment(f) <- envir
cache$set(key, f())
}
}