R/load-cache.R

## Create the cache env
cache <- NULL
initCache <- function () {
    cache <<- new.env(hash=TRUE)
}
initCache()

#' Save and load cache state
#'
#' Warm your query cache from a previous session by saving out the cache and
#' loading it back in.
#' @param file character file path to write the cache data to, in `.rds` format
#' @return Nothing; called for side effects.
#' @export
saveCache <- function (file) {
    saveRDS(cache, file=file)
}

#' @rdname saveCache
#' @export
loadCache <- function (file) {
    env <- readRDS(file)
    if (!is.environment(env)) {
        halt("'loadCache' requires an .rds file containing an environment")
    }
    ## Copy the values over
    for (key in ls(all.names=TRUE, envir=env)) {
        setCache(key, get(key, env))
    }
    invisible(NULL)
}

Try the httpcache package in your browser

Any scripts or data that you put into this service are public.

httpcache documentation built on Jan. 13, 2021, 7:46 p.m.