Skip to content

Instantly share code, notes, and snippets.

@kaneplusplus
Created April 2, 2014 20:58
Show Gist options
  • Save kaneplusplus/9943047 to your computer and use it in GitHub Desktop.
Save kaneplusplus/9943047 to your computer and use it in GitHub Desktop.
Memoize the result of an expression in R
require(rredis)
redisConnect()
memoize <- function(expr, key=NULL, expire_time=Inf, verbose=FALSE,
envir=parent.frame()) {
if (is.null(key)) {
key <- paste(substitute(expr), collapse="")
}
if (redisExists(key)) {
ret <- redisGet(key)
} else {
ret <- eval(substitute(expr), envir=envir)
redisSet(key, ret)
}
if (expire_time < Inf) {
redisExpireAt(proj_doc_key,
as.integer(as.POSIXct(Sys.time())+expire_time))
}
ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment