Invalidates cached values for a memoized function without affecting unrelated memo entries in shared storage.
Call without key to invalidate all cached values for that memo only.
Pass key to invalidate a single cached call:
a named/unnamed list of function arguments
a single character cache key hash
invalidate(f, key = NULL)Invisibly returns the memo function
library(magrittr)
base.dir <- file.path(tempdir(), "memofunc-example-invalidate")
unlink(base.dir, recursive = TRUE, force = TRUE)
old.options <- options(memofunc.storage.provider = list(name = "file", base.dir = base.dir))
counter <- new.env(parent = emptyenv())
counter$executed <- 0
expensive <- function (value) {
counter$executed <- counter$executed + 1
value * 10
}
memo.expensive <- memo(expensive, id = "invalidate-example")
# warm cache for two keys
memo.expensive(1)
#> [1] 10
memo.expensive(2)
#> [1] 20
# invalidate a single entry by arguments
invalidate(memo.expensive, list(value = 1))
counter$executed <- 0
memo.expensive(1)
#> [1] 10
memo.expensive(2)
#> [1] 20
counter$executed
#> [1] 1
# invalidate all entries for this memo only
invalidate(memo.expensive)
counter$executed <- 0
memo.expensive(1)
#> [1] 10
memo.expensive(2)
#> [1] 20
counter$executed
#> [1] 2
options(old.options)
unlink(base.dir, recursive = TRUE, force = TRUE)