R/cache.R

Defines functions clean_cache list_cache_files get_dir_size get_cache_dir

Documented in clean_cache get_cache_dir get_dir_size list_cache_files

# Cache functions

#' Returns the cache dir.
#'
#' @export
#' @return A string of the cache dir
get_cache_dir <- function() {
    tools::R_user_dir("rcldf", which = "cache")
}


#' Returns the filesize in bytes of a directory.
#'
#' @param path a directory to size
#'
#' @export
#' @return A numeric of the file size in bytes
get_dir_size <- function(path) {
    files <- list.files(path, full.names = TRUE)
    if (length(files) > 0) {
        return(sum(sapply(files, function(x) file.size(x))))
    }
    return(0)
}


#' Returns a dataframe of directories in the cache dir
#'
#' @export
#' @return A dataframe of the directories
list_cache_files <- function() {
    df <- list.files(get_cache_dir(), full.names=TRUE)
    df <- df[sapply(df, dir.exists, USE.NAMES=FALSE)]  # keep dirs only
    df <- data.frame(Path=df, Name=basename(df))
    df$Size <- sapply(df$Path, get_dir_size)
    df
}



#' Removes all files in the cache directory
#'
#' @export
#' @return A dataframe of the directories
clean_cache <- function() {
    cache_dir <- get_cache_dir()
    if (dir.exists(cache_dir)) {
        unlink(cache_dir, recursive=TRUE)
    }
}
SimonGreenhill/rcldf documentation built on April 25, 2024, 8:27 a.m.