Skip to content

Instantly share code, notes, and snippets.

@robertzk
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertzk/1cdb8e6a1945636e0ad2 to your computer and use it in GitHub Desktop.
Save robertzk/1cdb8e6a1945636e0ad2 to your computer and use it in GitHub Desktop.
cachemeifyoucan.R
fn2 <- function(id, model_version, type = 'loan_id') {
Reduce(rbind, lapply(id, function(id) {
seed <- as.integer(paste0("0x", substr(digest::digest(paste(id, model_version, type)), 1, 6)))
set.seed(seed)
data.frame(id = id, x = runif(1), y = rnorm(1))
}))
}
cache <- function(uncached_function, prefix, key, salt) {
cached_function <- new("function")
formals(cached_function) <- formals(uncached_function)
environment(cached_function) <-
list2env(list(prefix = prefix, key = key, salt = salt, uncached_function = uncached_function),
parent = environment(uncached_function))
body(cached_function) <- quote({
raw_call <- match.call()
call <- as.list(raw_call[-1])
true_salt <- call[intersect(names(call), salt)]
for (name in names(true_salt)) {
true_salt[[name]] <- eval.parent(true_salt[[name]])
}
true_salt <- digest::digest(true_salt)
table_name <- paste0(prefix, '_', true_salt)
cachemeifyoucan::execute(
cached_function_call(uncached_function, raw_call, parent.frame(), table_name, key)
)
})
cached_function
}
execute <- function(function_call) {
key <- eval(function_call$call[[function_call$key]], envir = function_call$context)
# Check which parts of key are already in function_call$table
cached_keys <- ...
uncached_keys <- setdiff(key, cached_keys)
cached_data <- grab from database using function_call$table and cached_keys
function_call$call[[function_call$key]] <- uncached_keys
uncached_data <- eval(function_call$call, envir = function_call$context)
# Save to database
# plyr::rbind.fill
}
cached_function_call <- function(fn, call, context, table, key) {
structure(list(fn = fn, call = call, context = context, table = table, key = key),
class = 'cached_function_call')
}
fn <- cache(fn2, prefix = 'blah_', key = 'id', salt = c('model_version', 'type'))
fn(1:3, 'default/en-US/1.0')
fn(1:3, 'default/en-US/1.0', type = 'customer_id') # should use a totally diff table
@iveksl2
Copy link

iveksl2 commented Jan 29, 2015

Can you provide More documentation when deployed, to provide greater readability

@robertzk
Copy link
Author

Agreed, this needs more documentation than there is code, plus heavy re-factoring

@robertzk
Copy link
Author

@FeiYeYe Can you try to make each method < 10 lines?

@FeiYeYe
Copy link

FeiYeYe commented Jan 29, 2015

certainly, i need to spend some time refactoring the current cache function which is much less general than this gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment