File: file-cache.R

package info (click to toggle)
r-cran-pkgload 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,164 kB
  • sloc: sh: 13; cpp: 9; ansic: 8; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 788 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Generate checksums for a vector of file paths.
# @keywords internal
md5 <- function(paths) {
  unlist(lapply(paths, tools::md5sum))
}

make_cache <- function() {
  .file_cache <- character()

  make <- function(paths) {
    paths <- path.expand(paths)
    new_hash <- md5(paths)
    old_hash <- .file_cache[paths]

    changed <- is.na(old_hash) | new_hash != old_hash
    .file_cache[paths[changed]] <<- new_hash[changed]

    paths[changed]
  }

  clear <- function() {
    .file_cache <<- character()
  }

  list(make = make, clear = clear)
}
.cache <- make_cache()

# Given vector of paths, return only those paths that have changed since the
# last invocation.
# @keywords internal
changed_files <- .cache$make

# Clear file cache.
# @keywords internal
clear_cache <- .cache$clear