Learn R Programming

MazamaCoreUtils (version 0.6.2)

manageCache: Manage cache size

Description

Remove old or excess files from a cache directory.

Usage

manageCache(
  cacheDir = NULL,
  extensions = c("html", "json", "pdf", "png"),
  maxCacheSize = 100,
  sortBy = "atime",
  maxFileAge = NULL
)

Value

Invisibly returns the number of files removed.

Arguments

cacheDir

Location of cache directory.

extensions

Vector of file extensions eligible for removal.

maxCacheSize

Maximum cache size in megabytes.

sortBy

Timestamp used to order files for size-based removal. One of "atime", "ctime", or "mtime".

maxFileAge

Maximum file age in days. Files with modification times older than this value are removed regardless of cache size. Fractional days are allowed.

Details

Files are eligible for removal when their extension matches extensions. Matching is case-sensitive and extensions may be supplied with or without a leading dot.

Files can be removed for two reasons:

  • files older than maxFileAge days are removed first

  • if the remaining cache exceeds maxCacheSize, additional files are removed until the cache is under the requested size

When removing files to satisfy maxCacheSize, files are ordered by the timestamp specified by sortBy.

Timestamp meanings are:

atime

File access time, updated when a file is opened.

ctime

File change time, updated when file metadata changes.

mtime

File modification time, updated when file contents change.

Examples

Run this code
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")])
}

Run the code above in your browser using DataLab