Suppose the following example:
library(xfun)
cp <- tempdir()
g <- function(x) x %% 2L
for(x in 1:2) {
i <- cache_rds(g(x), file = "my_cache.rds", dir = paste0(cp, "/"), hash = list(x))
print(list.files(cp, pattern = "my_cache.*\\.rds$"))
}
## Cache invalidated because x changes
# [1] "my_cache_f943c721ec5f66ac177ff5e146dc3f20.rds"
# [1] "my_cache_8d5bfb3a0b52e4a1e370feefa397cdea.rds"
## Cache stable if x does not change
i <- cache_rds(g(x), file = "my_cache.rds", dir = paste0(cp, "/"), hash = list(x))
print(list.files(cp, pattern = "my_cache.*\\.rds$"))
# [1] "my_cache_8d5bfb3a0b52e4a1e370feefa397cdea.rds"
## if we change definiton of g cache is not invalidated
g <- function(x) x + 1
i <- cache_rds(g(x), file = "my_cache.rds", dir = paste0(cp, "/"), hash = list(x))
print(list.files(cp, pattern = "my_cache.*\\.rds$"))
# [1] "my_cache_8d5bfb3a0b52e4a1e370feefa397cdea.rds"
## thus we need to add it to the hash
i <- cache_rds(g(x), file = "my_cache.rds", dir = paste0(cp, "/"), hash = list(x, g))
print(list.files(cp, pattern = "my_cache.*\\.rds$"))
# [1] "my_cache_6f873e4f042ac7d6879224ed9310af1b.rds"
## but now it changes every(!) time
i <- cache_rds(g(x), file = "my_cache.rds", dir = paste0(cp, "/"), hash = list(x, g))
print(list.files(cp, pattern = "my_cache.*\\.rds$"))
# [1] "my_cache_9b179bd66c0e8fe8f1194d62f2c2bafb.rds"
I am trying to make the invalidation of the cache depending on the definition of the external function g. Thus, I need to include it in the hash argument, but this causes the hash to invalidate with every call.
The only idea I have is to use deparse(g) to take a dependency on the code of g which works as expected:
i <- cache_rds(g(x), file = "my_cache.rds", dir = paste0(cp, "/"),
hash = list(x, deparse(g)))
print(list.files(cp, pattern = "my_cache.*\\.rds$"))
# [1] "my_cache_e33367d1a6af1356010fe6a24bcbe563.rds"
i <- cache_rds(g(x), file = "my_cache.rds", dir = paste0(cp, "/"),
hash = list(x, deparse(g)))
print(list.files(cp, pattern = "my_cache.*\\.rds$"))
# [1] "my_cache_e33367d1a6af1356010fe6a24bcbe563.rds"
g <- function(x) {
x
}
i <- cache_rds(g(x), file = "my_cache.rds", dir = paste0(cp, "/"),
hash = list(x, deparse(g)))
print(list.files(cp, pattern = "my_cache.*\\.rds$"))
# [1] "my_cache_84d8f7e4a7980ae6b2b26d1e01779259.rds"
but this feels a bit hackish. I was thus wondering what the canonical way would be to invalidate the cache based on the definition of an external function.
hash = list(x, g), does it change every time? Or just the first few times?list(x, g)does not resolve it.hash <- list(rlang::hash(1), rlang::hash(g))infeasible where 1 could bei?