Last active
August 29, 2015 14:14
-
-
Save robertzk/1cdb8e6a1945636e0ad2 to your computer and use it in GitHub Desktop.
cachemeifyoucan.R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Agreed, this needs more documentation than there is code, plus heavy re-factoring
@FeiYeYe Can you try to make each method < 10 lines?
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
Can you provide More documentation when deployed, to provide greater readability