R/cache_utils.R

Defines functions reset_cached_files is_too_old save_cache get_cached_obj

# helper functions for get_available_packages.R and get_description.R

get_cached_obj <- function(type, repo) {
  cache_dir <- tempdir()
  cache_file <- paste0(cache_dir, "/deepdep_", type, "_", repo, "_cache.RDS")
  if (!file.exists(cache_file) || is_too_old(cache_file)) {
    obj <- list()
    attr(obj, "type") <- type
    attr(obj, "repo") <- repo
    attr(obj, "new") <- TRUE
    saveRDS(obj, cache_file)
    obj
  } else {
    readRDS(cache_file)
  }
}

save_cache <- function(obj) {
  saveRDS(obj,
          paste0(tempdir(), "/deepdep_", attr(obj, "type"), "_", attr(obj, "repo"), "_cache.RDS"))
}

is_too_old <- function(file) {
   difftime(Sys.time(), file.info(file)$mtime, units = "secs") > 1800
}

reset_cached_files <- function(type) {
  cache_dir <- tempdir()
  cache_files <- paste0(cache_dir, "/deepdep_", type, "_",
                       c("CRAN", "bioc", "local"), "_cache.RDS")
  for (cache_file in cache_files) {
    if (file.exists(cache_file)) file.remove(cache_file)
  }
}

Try the deepdep package in your browser

Any scripts or data that you put into this service are public.

deepdep documentation built on June 22, 2024, 12:23 p.m.