% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/qs_cache.R
\name{qs_cache}
\alias{qs_cache}
\title{qcache}
\usage{
qs_cache(
  expr,
  name,
  envir = parent.frame(),
  cache_dir = ".cache",
  clear = FALSE,
  prompt = TRUE,
  qs_save_params = list(),
  qs_read_params = list(),
  verbose = TRUE
)
}
\arguments{
\item{expr}{The expression to evaluate.}

\item{name}{The cached expression name (see details).}

\item{envir}{The environment to evaluate \code{expr} in.}

\item{cache_dir}{The directory to store cached files in.}

\item{clear}{Set to \code{TRUE} to clear the cache (see details).}

\item{prompt}{Whether to prompt before clearing.}

\item{qs_save_params}{List of parameters passed on to \code{qs_save}.}

\item{qs_read_params}{List of parameters passed on to \code{qs_read}.}

\item{verbose}{Boolean. If \code{TRUE}, the function prints an informative message regarding the status of cached objects.}
}
\description{
Helper function for caching objects for long running tasks
}
\details{
This is a (very) simple helper function to cache results of long running calculations. There are other packages specializing
in caching data that are more feature complete.

The evaluated expression is saved with \code{\link[=qs_save]{qs_save()}} in \verb{<cache_dir>/<name>.qs2}.
If the file already exists instead, the expression is not evaluated and the cached result is read using \code{\link[=qs_read]{qs_read()}} and returned.

To clear a cached result, you can manually delete the associated \code{.qs2} file, or you can call \code{\link[=qs_cache]{qs_cache()}} with \code{clear = TRUE}.
If \code{prompt} is also \code{TRUE} a prompt will be given asking you to confirm deletion.
If \code{name} is not specified, all cached results in \code{cache_dir} will be removed.
}
\examples{
cache_dir <- tempdir()

a <- 1
b <- 5

# not cached
result <- qs_cache({a + b},
                 name="aplusb",
                 cache_dir = cache_dir,
                 qs_save_params = list(compress_level = 5))

# cached
result <- qs_cache({a + b},
                 name="aplusb",
                 cache_dir = cache_dir,
                 qs_save_params = list(compress_level = 5))

# clear cached result
qs_cache(name="aplusb", clear=TRUE, prompt=FALSE, cache_dir = cache_dir)
}
