% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/cacheManagement.R
\name{manageCache}
\alias{manageCache}
\title{Manage cache size}
\usage{
manageCache(
  cacheDir = NULL,
  extensions = c("html", "json", "pdf", "png"),
  maxCacheSize = 100,
  sortBy = "atime",
  maxFileAge = NULL
)
}
\arguments{
\item{cacheDir}{Location of cache directory.}

\item{extensions}{Vector of file extensions eligible for removal.}

\item{maxCacheSize}{Maximum cache size in megabytes.}

\item{sortBy}{Timestamp used to order files for size-based removal. One of
\code{"atime"}, \code{"ctime"}, or \code{"mtime"}.}

\item{maxFileAge}{Maximum file age in days. Files with modification times
older than this value are removed regardless of cache size. Fractional days
are allowed.}
}
\value{
Invisibly returns the number of files removed.
}
\description{
Remove old or excess files from a cache directory.
}
\details{
Files are eligible for removal when their extension matches \code{extensions}.
Matching is case-sensitive and extensions may be supplied with or without a
leading dot.

Files can be removed for two reasons:

\itemize{
\item files older than \code{maxFileAge} days are removed first
\item if the remaining cache exceeds \code{maxCacheSize}, additional files are
removed until the cache is under the requested size
}

When removing files to satisfy \code{maxCacheSize}, files are ordered by the
timestamp specified by \code{sortBy}.

Timestamp meanings are:

\describe{
\item{\code{atime}}{File access time, updated when a file is opened.}
\item{\code{ctime}}{File change time, updated when file metadata changes.}
\item{\code{mtime}}{File modification time, updated when file contents change.}
}
}
\examples{
CACHE_DIR <- tempdir()

write.csv(matrix(1, 400, 500), file = file.path(CACHE_DIR, "m1.csv"))
write.csv(matrix(2, 400, 500), file = file.path(CACHE_DIR, "m2.csv"))
write.csv(matrix(3, 400, 500), file = file.path(CACHE_DIR, "m3.csv"))
write.csv(matrix(4, 400, 500), file = file.path(CACHE_DIR, "m4.csv"))

for (file in list.files(CACHE_DIR, pattern = "\\\\.csv$", full.names = TRUE)) {
  print(file.info(file)[, c("size", "mtime")])
}

# Remove files based on access time until the cache is under 1 MB
manageCache(
  CACHE_DIR,
  extensions = "csv",
  maxCacheSize = 1,
  sortBy = "atime"
)

for (file in list.files(CACHE_DIR, pattern = "\\\\.csv$", full.names = TRUE)) {
  print(file.info(file)[, c("size", "mtime")])
}

}
