Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Created September 16, 2020 00:20
Show Gist options
  • Select an option

  • Save jimbrig/faf4181ae8d3e7bc97f37fc407f61523 to your computer and use it in GitHub Desktop.

Select an option

Save jimbrig/faf4181ae8d3e7bc97f37fc407f61523 to your computer and use it in GitHub Desktop.
cache_list <- function() {
list.files(
cache_path(),
pattern = ".qs",
ignore.case = TRUE,
recursive = TRUE,
full.names = TRUE
)
}
cache_delete <- function(files, force = TRUE) {
if (!all(file.exists(files))) {
stop("These files don't exist or can't be found: \n",
strwrap(files[!file.exists(files)], indent = 5), call. = FALSE)
}
unlink(files, force = force, recursive = TRUE)
}
cache_delete_all <- function(force = TRUE) {
files <- list.files(cache+path(), pattern = ".qs", ignore.case = TRUE,
full.names = TRUE, recursive = TRUE)
unlink(files, force = force, recursive = TRUE)
}
cache_details <- function(files = NULL) {
if (is.null(files)) {
files <- list.files(cache_path(), pattern = ".qs", ignore.case = TRUE,
full.names = TRUE, recursive = TRUE)
structure(lapply(files, file_info_), class = "cache_info")
} else {
structure(lapply(files, file_info_), class = "cache_info")
}
}
file_info_ <- function(x) {
fs <- file.size(x)
list(file = x,
type = "qs",
size = if (!is.na(fs)) getsize(fs) else NA
)
}
getsize <- function(x) {
round(x/10 ^ 6, 3)
}
print.cache_info <- function(x, ...) {
cat("<cached files>", sep = "\n")
cat(sprintf(" directory: %s\n", cache_path()), sep = "\n")
for (i in seq_along(x)) {
cat(paste0(" file: ", sub(cache_path(), "", x[[i]]$file)), sep = "\n")
cat(paste0(" size: ", x[[i]]$size, " mb"), sep = "\n")
cat("\n")
}
}
cache_path <- function() rappdirs::user_cache_dir("cache")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment