% Generated by tinyroxygen: do not edit by hand
% Please edit documentation in cache.R
\name{dcache}
\alias{dcache}
\title{Create a Disk Cache}
\usage{
dcache(
  dir = NULL,
  max_size = Inf,
  max_age = Inf,
  max_n = Inf,
  evict = c("lru", "fifo"),
  missing = key_missing(),
  destroy_on_finalize = FALSE
)
}

\arguments{
\item{dir}{Directory used to store the cached files. If \code{NULL} (the
  default), a new temporary directory is created and used.}

\item{max_size}{See \code{\link{mcache}}.}

\item{max_age}{See \code{\link{mcache}}.}

\item{max_n}{See \code{\link{mcache}}.}

\item{evict}{See \code{\link{mcache}}.}

\item{missing}{See \code{\link{mcache}}.}

\item{destroy_on_finalize}{If \code{TRUE}, the cache directory and all of
  its contents are deleted from disk when the returned object is garbage
  collected. Default \code{FALSE}.}
}

\value{
A \code{dcache} object with the same methods as
  \code{\link{mcache}}, plus \code{destroy()}, which deletes the cache
  directory from disk.
}

\description{
A lightweight disk-backed key-value cache: each value is
  stored as its own \code{.rds} file inside \code{dir}. Pruning is not
  throttled, and eviction metadata (age, last-access time) comes directly
  from the files' own modification times rather than being tracked
  separately.
}

\examples{
cache <- dcache(dir = tempdir())

fit_model <- function(n, cache) {
  key <- hash(n)
  cached <- cache$get(key)
  if (!is.key_missing(cached)) {
    return(cached)
  }

  set.seed(123)
  mydata <- data.frame(x = seq_len(n), y = seq_len(n) * 2 + rnorm(n))
  mycoef <- coef(lm(y ~ x, data = mydata))

  cache$set(key, mycoef)
  mycoef
}

fit_model(100, cache) # computed and cached
fit_model(100, cache) # reused from disk, no recomputation
}

