Skip to content

Commit

Permalink
Hash the function body as well as the arguments
Browse files Browse the repository at this point in the history
This ensures functions with identical arguments will not have the same
cache.

Fixes #38
  • Loading branch information
jimhester committed Apr 20, 2017
1 parent 884d565 commit 70d959b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Version 1.0.0

* Caches now hash the function body along with the arguments, to ensure
functions with identical arguments use a separate file-system cache. (#38)
* Handle missing arguments in memoised functions for simple cases not using
non-standard-evaluation (#19).
* `memoise()` gains a `cache=` argument to specify an external cache. Two types
Expand Down
2 changes: 1 addition & 1 deletion R/memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ memoise <- memoize <- function(f, ..., envir = environment(f), cache = cache_mem
args <- c(lapply(called_args, eval, parent.frame()),
lapply(default_args, eval, envir = environment()))

hash <- `_cache`$digest(c(args,
hash <- `_cache`$digest(c(body(`_f`), args,
lapply(`_additional`, function(x) eval(x[[2L]], environment(x)))))

if (`_cache`$has_key(hash)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-filesystem.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,17 @@ test_that("using a filesystem cache works", {
expect_true(is.memoised(fnm))
expect_false(is.memoised(fn))
})

test_that("two functions with the same arguments produce different caches (#38)", {

temp <- tempfile()
fs <- cache_filesystem(temp)

f1 <- memoise(function() 1, cache = fs)
f2 <- memoise(function() 2, cache = fs)

expect_equal(f1(), 1)
expect_equal(f2(), 2)

expect_equal(length(list.files(temp)), 2)
})

0 comments on commit 70d959b

Please sign in to comment.