Skip to content

Commit

Permalink
feat: don't adjust prune, better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dsweber2 committed Sep 6, 2023
1 parent 829638d commit f0a7cd4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
15 changes: 3 additions & 12 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ cache_environ$epidatr_cache <- NULL
#' dir = "some/subdir",
#' days = 14,
#' max_size = 512,
#' logfile = "some/subdir/logs.txt",
#' prune_rate = 20L
#' logfile = "some/subdir/logs.txt"
#' )
#' }
#'
Expand All @@ -90,11 +89,6 @@ cache_environ$epidatr_cache <- NULL
#' @param logfile where cachem's log of transactions is stored, relative to the
#' cache directory. By default, it is `"logfile.txt"`. The environmental
#' variable is `EPIDATR_CACHE_LOGFILE`.
#' @param prune_rate how many calls to go between checking if any cache elements
#' are too old or if the cache overall is too large. Defaults to `2000L`.
#' Since cachem fixes the max time between prune checks to 5 seconds, there's
#' little reason to actually change this parameter. Doesn't have a
#' corresponding environmental variable.
#' @param confirm whether to confirm directory creation. default is `TRUE`;
#' should only be set in non-interactive scripts
#' @seealso [clear_cache] to delete the old cache while making a new one,
Expand All @@ -107,7 +101,6 @@ set_cache <- function(cache_dir = NULL,
days = NULL,
max_size = NULL,
logfile = NULL,
prune_rate = 2000L,
confirm = TRUE) {
if (is.null(cache_dir) && sessionInfo()$R.version$major >= 4) {
cache_dir <- Sys.getenv("EPIDATR_CACHE_DIR", unset = tools::R_user_dir("epidatr"))
Expand All @@ -129,7 +122,7 @@ set_cache <- function(cache_dir = NULL,
logfile <- Sys.getenv("EPIDATR_CACHE_LOGFILE", unset = "logfile.txt")
}
stopifnot(is.character(logfile))
stopifnot(is.numeric(days), is.numeric(max_size), is.integer(prune_rate))
stopifnot(is.numeric(days), is.numeric(max_size))
#
# make sure that that directory exists and drag the user into that process
cache_exists <- file.exists(cache_dir)
Expand Down Expand Up @@ -171,8 +164,7 @@ set_cache <- function(cache_dir = NULL,
dir = cache_dir,
max_size = as.integer(max_size * 1024^2),
max_age = days * 24 * 60 * 60,
logfile = file.path(cache_dir, logfile),
prune_rate = prune_rate
logfile = file.path(cache_dir, logfile)
)
}
}
Expand All @@ -190,7 +182,6 @@ set_cache <- function(cache_dir = NULL,
#' days = 14,
#' max_size = 512,
#' logfile = "some/subdir/logs.txt",
#' prune_rate = 20L
#' )
#' }
#' @param disable instead of setting a new cache, disable caching entirely;
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ reference:
- epirange
- timeset
- title: Control Caching behavior
desc: configure an optional persistent cache
- contents:
- set_cache
- clear_cache
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ test_set_cache <- function(cache_dir = new_temp_dir,
days = 1,
max_size = 1,
logfile = "logfile.txt",
prune_rate = 3L,
confirm = FALSE) {
set_cache(
cache_dir = cache_dir,
days = days,
max_size = max_size,
logfile = logfile,
prune_rate = prune_rate,
confirm = confirm
)
}
Expand All @@ -41,7 +39,7 @@ test_that("cache set as expected", {
}
expect_equal(cache_info()$max_size, 1024^2)
expect_equal(cache_info()$max_age, 24 * 60 * 60)
expect_equal(cache_info()$prune_rate, 3)
expect_equal(cache_info()$prune_rate, 20)
expect_equal(cache_info()$logfile, file.path(new_temp_dir, "logfile.txt"))
expect_equal(cache_info()$evict, "lru")
expect_equal(cache_info()$max_n, Inf)
Expand Down Expand Up @@ -198,6 +196,7 @@ test_that("check_is_cachable", {
})

test_set_cache()
cache_environ$epidatr_cache$prune()
clear_cache(disable = TRUE)
rm(new_temp_dir)

Expand Down

0 comments on commit f0a7cd4

Please sign in to comment.