Closed DavZim closed 4 years ago
Is it possible to have a function is_cached that checks if a call is already in the cache?
is_cached
Something like this (doesn't work, as I don't fully understand the internals of memoise()).
memoise()
is_cached <- function(f, ...) { stopifnot(is.memoised(f)) args <- list(...) cc <- environment(f)$`_cache` cc$has_key(cc$digest(c(body(f), args))) } myfun <- memoise(function(name = "Alice") print(paste("Hello", name))) myfun("Alice") is_cached(myfun, "Alice") # TRUE is_cached(myfun, name = "Alice") # TRUE is_cached(myfun, name = "Bob") # FALSE
Even better: if the is_cached function is able to take the call to the memoised function is_cached(myfun("Alice")).
is_cached(myfun("Alice"))
memoise::has_cache()?
memoise::has_cache()
I didn't see that! Works as expected. Thanks for the hint!
Is it possible to have a function
is_cached
that checks if a call is already in the cache?Something like this (doesn't work, as I don't fully understand the internals of
memoise()
).Even better: if the
is_cached
function is able to take the call to the memoised functionis_cached(myfun("Alice"))
.