Skip to content

Commit f4d0584

Browse files
committed
docs+test: cache docs and tests
1 parent c9abe5d commit f4d0584

11 files changed

+235
-35
lines changed

R/cache.R

+21-15
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
cache_environ <- new.env(parent = emptyenv())
44
cache_environ$use_cache <- NULL
55
cache_environ$epidatr_cache <- NULL
6-
#' create a new cache for this session
6+
#' create or renew a cache for this session
77
#'
88
#' @description
9-
#' `set_cache` (re)defines the cache to use. This does not clear existing data at any previous location, but defines a new access for this R session.
10-
#' Say your cache is normally stored in the default directory, but for the current session you want to save your results in `~/my/temporary/savedirectory`, then you would call `set_cache(dir = "~/my/temporary/savedirectory")`.
11-
#' Or if you know the data from 2 days ago is wrong, you could call `set_cache(days = 1)` to clear older data. In both cases, these changes would only last for a single session.
12-
#' In general, it is better to set your preferences via environmental variables in your `.Renviron` folder, with the corresponding variables listed in the arguments section below.
13-
#' In addition to those, there is the `EPIDATR_USE_CACHE` environmental variable, which unless defined to be `TRUE` otherwise defaults to `FALSE`.
9+
#' By default, epidatr re-requests data from the API on every call of `fetch`. In case you find yourself repeatedly calling the same data, you can enable the cache using either this function for a given session, or environmental variables for a persistent cache.
10+
#' The typical recommended workflow for using the cache is to set the environmental variables `EPIDATR_USE_CACHE=TRUE` and `EPIDATR_CACHE_DIRECTORY="/your/directory/here"`in your `.Renviron`, for example by calling `usethis::edit_r_environ()`.
11+
#' See the parameters below for some more configurables if you're so inclined.
12+
#'
13+
#' `set_cache` (re)defines the cache to use in a particular R session. This does not clear existing data at any previous location, but instead creates a handle to the new cache using [cachem](https://cachem.r-lib.org/index.html) that seamlessly handles caching for you.
14+
#' Say your cache is normally stored in some default directory, but for the current session you want to save your results in `~/my/temporary/savedirectory`, then you would call `set_cache(dir = "~/my/temporary/savedirectory")`.
15+
#' Or if you know the data from 2 days ago is wrong, you could call `set_cache(days = 1)` to clear older data whenever the cache is referenced.
16+
#' In both cases, these changes would only last for a single session (though the deleted data would be gone permanently!).
1417
#'
1518
#' An important feature of the caching in this package is that only calls which specify either `issues` before a certain date, or `as_of` before a certain date will actually cache. For example the call
1619
#' ```
@@ -35,7 +38,7 @@ cache_environ$epidatr_cache <- NULL
3538
#' as_of = "2023-08-01"
3639
#' )
3740
#' ```
38-
#' *will* cache, since normal new versions of data can't invalidate it. It is still possible that Delphi may patch such data, but the frequency is on the order of months rather than days. We are working on creating a public channel to communicate such updates. Stars for `issues` won't cache, since they're subject to cache invalidation by normal versioning.
41+
#' *will* cache, since normal new versions of data can't invalidate it (since they would be `as_of` a later date). It is still possible that Delphi may patch such data, but the frequency is on the order of months rather than days. We are working on creating a public channel to communicate such updates. While specifying `issues` will usually cache, a call with `issues="*"` won't cache, since its subject to cache invalidation by normal versioning.
3942
#'
4043
#' On the backend, the cache uses cachem, with filenames generated using an md5 encoding of the call url. Each file corresponds to a unique epidata-API call.
4144
#' @examples
@@ -49,12 +52,13 @@ cache_environ$epidatr_cache <- NULL
4952
#' )
5053
#' }
5154
#'
52-
#' @param cache_dir the directory in which the cache is stored. By default, this is `tools::R_user_dir()` if on R 4.0+, but must be specified for earlier versions of R. The path can be either relative or absolute. The environmental variable is `EPIDATR_CACHE_DIR`
53-
#' @param days the maximum length of time in days to keep any particular cached call. By default this is `1`. The environmental variable is `EPIDATR_CACHE_MAX_AGE_DAYS`
55+
#' @param cache_dir the directory in which the cache is stored. By default, this is `tools::R_user_dir()` if on R 4.0+, but must be specified for earlier versions of R. The path can be either relative or absolute. The environmental variable is `EPIDATR_CACHE_DIR`.
56+
#' @param days the maximum length of time in days to keep any particular cached call. By default this is `1`. The environmental variable is `EPIDATR_CACHE_MAX_AGE_DAYS`.
5457
#' @param max_size the size of the entire cache, in MB, at which to start pruning entries. By default this is `1024`, or 1GB. The environmental variable is `EPIDATR_CACHE_MAX_SIZE_MB`.
5558
#' @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`.
5659
#' @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.
57-
#' @param confirm whether to confirm directory creation. default is `TRUE`; should only be set in scripts
60+
#' @param confirm whether to confirm directory creation. default is `TRUE`; should only be set in non-interactive scripts
61+
#' @seealso [clear_cache] to delete the old cache while making a new one, [disable_cache] to disable without deleting, and [cache_info]
5862
#' @export
5963
#' @import cachem
6064
set_cache <- function(cache_dir = NULL,
@@ -122,9 +126,7 @@ set_cache <- function(cache_dir = NULL,
122126
}
123127
}
124128

125-
126129
#' manually reset the cache, deleting all currently saved data and starting afresh
127-
#'
128130
#' @description
129131
#' deletes the current cache and resets a new cache. Deletes local data! If you are using a session unique cache, you will have to pass the arguments you used for `set_cache` earlier, otherwise the system-wide `.Renviron`-based defaults will be used.
130132
#' @examples
@@ -137,7 +139,6 @@ set_cache <- function(cache_dir = NULL,
137139
#' prune_rate = 20L
138140
#' )
139141
#' }
140-
#'
141142
#' @param disable instead of setting a new cache, disable caching entirely; defaults to `FALSE`
142143
#' @inheritParams set_cache
143144
#' @seealso [set_cache] to start a new cache (and general caching info), [disable_cache] to only disable without deleting, and [cache_info]
@@ -154,19 +155,24 @@ clear_cache <- function(disable = FALSE, ...) {
154155
#' @description
155156
#' Disable caching until you call `set_cache` or restart R. The files defining the cache are untouched. If you are looking to disable the caching more permanently, set `EPIDATR_USE_CACHE=FALSE` as environmental variable in your `.Renviron`.
156157
#' @export
158+
#' @seealso [set_cache] to start a new cache (and general caching info), [clear_cache] to delete the cache and set a new one, and [cache_info]
159+
#' @import cachem
157160
disable_cache <- function() {
158161
cache_environ$epidatr_cache <- NULL
159162
}
160163

161-
#' turn off the caching for this session
164+
#' describe current cache
162165
#' @description
163166
#' Print out the information about the cache (as would be returned by cachem's `info()` method)
167+
#' @examples
168+
#' cache_info()
169+
#' @seealso [set_cache] to start a new cache (and general caching info), [clear_cache] to delete the cache and set a new one, and [disable_cache] to disable without deleting
164170
#' @export
165171
cache_info <- function() {
166172
cache_environ$epidatr_cache$info()
167173
}
168174

169-
#' create a new cache for this session
175+
#' dispatch caching
170176
#'
171177
#' @description
172178
#' the guts of caching, its interposed between fetch and the specific fetch methods. Internal method only.

_pkgdown.yml

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ reference:
1515
- avail_endpoints
1616
- epirange
1717
- timeset
18+
- title: Control Caching behavior
19+
- contents:
20+
- set_cache
21+
- clear_cache
22+
- disable_cache
23+
- cache_info
1824
- title: Make API requests
1925
desc: Query Delphi Epidata endpoints
2026
- contents:

man/cache_epidata_call.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cache_info.Rd

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_is_cachable.Rd

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_is_recent.Rd

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/clear_cache.Rd

+7-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/disable_cache.Rd

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/set_cache.Rd

+19-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/setup.R

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
current_cache <- cache_environ$epidatr_cache
2+
disable_cache()
3+
withr::defer(cache_environ$epidatr_cache <- current_cache, teardown_env())

0 commit comments

Comments
 (0)