Skip to content

Commit 309b7e2

Browse files
committed
fix: clear_cache reuses previous set_cache settings
1 parent 3e9f279 commit 309b7e2

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

R/cache.R

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
cache_environ <- new.env(parent = emptyenv())
44
cache_environ$use_cache <- NULL
55
cache_environ$epidatr_cache <- NULL
6+
cache_environ$cache_args <- NULL
67

78
#' Create or renew a cache for this session
89
#' @aliases set_cache
@@ -169,6 +170,12 @@ set_cache <- function(cache_dir = NULL,
169170
max_age = days * 24 * 60 * 60,
170171
logfile = file.path(cache_dir, logfile)
171172
)
173+
cache_environ$cache_args <- list2(
174+
cache_dir = cache_dir,
175+
days = days,
176+
max_size = max_size,
177+
logfile = logfile
178+
)
172179
}
173180

174181
cli::cli_inform(c(
@@ -183,9 +190,9 @@ set_cache <- function(cache_dir = NULL,
183190
#' Manually reset the cache, deleting all currently saved data and starting afresh
184191
#' @description
185192
#' Deletes the current cache and resets a new cache. Deletes local data! If you
186-
#' are using a session unique cache, you will have to pass the arguments you
187-
#' used for `set_cache` earlier, otherwise the system-wide `.Renviron`-based
188-
#' defaults will be used.
193+
#' are using a session unique cache, the previous settings will be reused. If
194+
#' you pass in new `set_cache` arguments, they will take precedence over the
195+
#' previous settings.
189196
#' @param disable instead of setting a new cache, disable caching entirely;
190197
#' defaults to `FALSE`
191198
#' @inheritDotParams set_cache
@@ -198,11 +205,22 @@ set_cache <- function(cache_dir = NULL,
198205
clear_cache <- function(..., disable = FALSE) {
199206
if (any(!is.na(cache_environ$epidatr_cache))) {
200207
cache_environ$epidatr_cache$destroy()
208+
recovered_args <- cache_environ$cache_args
209+
cache_environ$cache_args <- NULL
210+
} else {
211+
recovered_args <- list()
201212
}
213+
args <- rlang::dots_list(
214+
...,
215+
confirm = FALSE,
216+
!!!recovered_args,
217+
.homonyms = "first",
218+
.ignore_empty = "all"
219+
)
202220
if (disable) {
203221
cache_environ$epidatr_cache <- NULL
204222
} else {
205-
set_cache(...)
223+
rlang::inject(set_cache(!!!args))
206224
}
207225
}
208226

tests/testthat/test-cache.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
test_that("basic cache setup", {
2-
# Should be off at the start
3-
expect_true(is.null(cache_environ$epidatr_cache))
4-
})
5-
61
new_temp_dir <- tempdir()
72
test_set_cache <- function(cache_dir = new_temp_dir,
83
days = 1,
@@ -18,6 +13,11 @@ test_set_cache <- function(cache_dir = new_temp_dir,
1813
)
1914
}
2015

16+
test_that("basic cache setup", {
17+
# Should be off at the start
18+
expect_true(is.null(cache_environ$epidatr_cache))
19+
})
20+
2121
test_that("cache set as expected", {
2222
# Setup a new cache
2323
expect_message(test_set_cache())
@@ -85,7 +85,7 @@ test_that("cache saves & loads", {
8585
expect_equal(first_call, direct_from_cache[[1]])
8686

8787
# Test the empty return branch
88-
expect_message(clear_cache(confirm = FALSE))
88+
expect_message(clear_cache())
8989
local_mocked_bindings(
9090
# see generate_test_data.R
9191
content = function(...) {

0 commit comments

Comments
 (0)