Skip to content
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

Function to check if a function call is cached #112

Closed
DavZim opened this issue Aug 13, 2020 · 2 comments
Closed

Function to check if a function call is cached #112

DavZim opened this issue Aug 13, 2020 · 2 comments

Comments

@DavZim
Copy link

DavZim commented Aug 13, 2020

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()).

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")).

@jimhester
Copy link
Member

memoise::has_cache()?

@DavZim
Copy link
Author

DavZim commented Aug 13, 2020

I didn't see that! Works as expected. Thanks for the hint!

@DavZim DavZim closed this as completed Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants