If cacheDir takes up more than maxCacheSize megabytes on disk, files will be removed in order of access time by default. Only files matching extensions are eligible for removal. Files can also be removed in order of change time with sortBy='ctime' or modification time with sortBy='mtime'.

The maxFileAge parameter can also be used to remove files that haven't been modified in a certain number of days. Fractional days are allowed. This removal happens without regard to the size of the cache and is useful for removing out-of-date data.

It is important to understand precisely what these timestamps represent:

  • atime -- File access time: updated whenever a file is opened.

  • ctime -- File change time: updated whenever a file's metadata changes e.g. name, permission, ownership.

  • mtime -- file modification time: updated whenever a file's contents change.

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

Arguments

cacheDir

Location of cache directory.

extensions

Vector of file extensions eligible for removal.

maxCacheSize

Maximum cache size in megabytes.

sortBy

Timestamp to sort by when sorting files eligible for removal. One of atime|ctime|mtime.

maxFileAge

Maximum age in days of files allowed in the cache.

Value

Invisibly returns the number of files removed.

Examples

library(MazamaCoreUtils)

# Create a cache directory and fill it with 1.6 MB of data
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, full.names=TRUE)) {
  print(file.info(file)[,c(1,6)])
}
#>                                                                      size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/downlit  384
#>                                                                                    atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/downlit 2024-02-07 10:47:40
#>                                                                              size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d12f7e7d  295
#>                                                                                            atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d12f7e7d 2024-02-07 10:47:46
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d26412df2  437
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d26412df2 2024-02-07 10:47:46
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d334327c9  465
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d334327c9 2024-02-07 10:47:47
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d3b3eab26  223
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d3b3eab26 2024-02-07 10:47:40
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d7267c4a0  227
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d7267c4a0 2024-02-07 10:47:43
#>                                                                       size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m1.csv 405687
#>                                                                                   atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m1.csv 2024-02-07 10:47:49
#>                                                                       size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m2.csv 405687
#>                                                                                   atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m2.csv 2024-02-07 10:47:49
#>                                                                       size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m3.csv 405687
#>                                                                                   atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m3.csv 2024-02-07 10:47:49
#>                                                                       size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m4.csv 405687
#>                                                                                   atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m4.csv 2024-02-07 10:47:49

# Remove files based on access time until we get under 1 MB
manageCache(CACHE_DIR, extensions='csv', maxCacheSize=1, sortBy='atime')
for (file in list.files(CACHE_DIR, full.names=TRUE)) {
  print(file.info(file)[,c(1,6)])
}
#>                                                                      size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/downlit  384
#>                                                                                    atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/downlit 2024-02-07 10:47:40
#>                                                                              size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d12f7e7d  295
#>                                                                                            atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d12f7e7d 2024-02-07 10:47:46
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d26412df2  437
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d26412df2 2024-02-07 10:47:46
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d334327c9  465
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d334327c9 2024-02-07 10:47:47
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d3b3eab26  223
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d3b3eab26 2024-02-07 10:47:40
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d7267c4a0  227
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d7267c4a0 2024-02-07 10:47:43
#>                                                                       size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m3.csv 405687
#>                                                                                   atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m3.csv 2024-02-07 10:47:49
#>                                                                       size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m4.csv 405687
#>                                                                                   atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m4.csv 2024-02-07 10:47:49

# Or remove files based on modification time
manageCache(CACHE_DIR, extensions='csv', maxCacheSize=1, sortBy='mtime')
for (file in list.files(CACHE_DIR, full.names=TRUE)) {
  print(file.info(file)[,c(1,6)])
}
#>                                                                      size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/downlit  384
#>                                                                                    atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/downlit 2024-02-07 10:47:40
#>                                                                              size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d12f7e7d  295
#>                                                                                            atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d12f7e7d 2024-02-07 10:47:46
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d26412df2  437
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d26412df2 2024-02-07 10:47:46
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d334327c9  465
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d334327c9 2024-02-07 10:47:47
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d3b3eab26  223
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d3b3eab26 2024-02-07 10:47:40
#>                                                                               size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d7267c4a0  227
#>                                                                                             atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/file404d7267c4a0 2024-02-07 10:47:43
#>                                                                       size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m3.csv 405687
#>                                                                                   atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m3.csv 2024-02-07 10:47:49
#>                                                                       size
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m4.csv 405687
#>                                                                                   atime
#> /var/folders/84/y311lppx4519st7n8_gfkd1w0000gn/T//RtmpkHiqRu/m4.csv 2024-02-07 10:47:49