R/mdl-cache-paths.R

Defines functions mdl_get_cache_filename mdl_get_cache_dir

Documented in mdl_get_cache_dir mdl_get_cache_filename

#' Get the Cache Directory
#'
#' The cache directory can be set in the config-file (moodleR:->cache_dir:), which is recommended.
#' If it is not present, or no config is found, a tempdir() will be returned.
#'
#' @import rlang
#' @importFrom  glue glue
#' @return Character vector with path
#' @export
mdl_get_cache_dir <- function(){
  ret <- tryCatch(
    config::get("moodleR")$cache_dir,
    error = function(e){NULL}
  )
  if (is.null(ret)) {
    ret <- tempdir()
    if (!file.exists(ret)) {
      dir.create(ret)
      rlang::inform(glue::glue("Directory created: {ret}"))
    }
    rlang::inform(message =
                    glue::glue("Directory '{ret}' used by default. Set config variable in moodleR:mdl_cache_dir to override.") ,
                  .frequency = "once",
                  .frequency_id = "mdl_get_cache_dir"
    )
  }
  ret
}


#' Get the Cache Filename
#'
#' The cache filename ("mdl_cache.sqlite" by default), can be set in the config-file((moodleR:->cache_dir:).
#' @import rlang
#' @importFrom glue glue
#' @return Character vector with path
#' @export
mdl_get_cache_filename <- function() {
  ret <- tryCatch(
    config::get("moodleR")$cache_filename,
    error = function(e){NULL}
  )
  if (is.null(ret)) {
    ret <- "mdl_cache.sqlite"
    rlang::inform(
      message =
        glue::glue("Filename '{ret}' used by default. Set config variable in moodleR:mdl_cache_dir to override."),
      .frequency = "once",
      .frequency_id = "mdl_get_cache_filename"
    )
  }
  ret
}

Try the moodleR package in your browser

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

moodleR documentation built on Aug. 15, 2022, 5:09 p.m.