You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#' to reuse this cache, the environmental variables need to be set
13
-
#' Due to the 80 character limit on filenames in cachem, the cache for a given api call will be stored in the `md5` hash of the filename. For example,
10
+
#' `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.
11
+
#' 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")`.
12
+
#' 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.
13
+
#' 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.
14
+
#' In addition to those, there is the `EPIDATR_USE_CACHE` environmental variable, which unless defined to be `TRUE` otherwise defaults to `FALSE`.
14
15
#'
16
+
#' 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.
15
17
#' @examples
16
-
#' set_cache()
18
+
#' \dontrun{
19
+
#' set_cache(
20
+
#' dir = "some/subdir",
21
+
#' days = 14,
22
+
#' max_size = 512,
23
+
#' logfile = "some/subdir/logs.txt",
24
+
#' prune_rate = 20L
25
+
#' )
26
+
#' }
17
27
#'
18
-
#' @param dir
19
-
#' @param days
20
-
#' @param max_size
21
-
#' @param logfile
22
-
#' @param prune_rate
23
-
set_cache<-function(dir=NULL,
28
+
#' @param dir the directory in which the cache is stored. By default, this is `here::here(".epidatr_cache")`. The environmental variable is `EPIDATR_CACHE_DIR`
29
+
#' @param days the maximum length of time in days to keep any particular cached call. By default this is `7`
30
+
#' @param max_size the size of the entire cache, in MB, at which to start pruning entries.
31
+
#' @param logfile where cachem's log of transactions is stored. By default, it is `file.path(dir, "logfile.txt")`, so it's contained in the cache's directory. The environmental variable is `EPIDATR_CACHE_LOGFILE`
32
+
#' @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.
# make sure that that directory exists and drag the user into that process
60
+
cache_exists<- file.exists(cache_dir)
61
+
cache_usable<- file.access(cache_dir, mode=6) ==0
62
+
if (!(cache_exists)) {
63
+
user_input<- readline(glue::glue("there is no directory at {cache_dir}; the cache will be turned off until a viable directory has been set. Create one? (yes|no) "))
print(glue::glue("The directory at {cache_dir} is not accessible; check permissions and/or use a different directory for the cache (see the `set_cache` documentation)."))
81
+
} elseif (cache_exists) {
82
+
cache_environ$epidatr_cache<-cachem::cache_disk(
83
+
dir=cache_dir,
84
+
max_size= as.integer(max_size*1024^2),
85
+
max_age=days*24*60*60,
86
+
logfile=logfile,
87
+
prune_rate=prune_rate
88
+
)
89
+
}
51
90
}
52
91
53
-
#' to manually reset the cache, deleting the currently saved data and starting fresh, call `epidatr_cache$destroy()`
92
+
93
+
#' manually reset the cache, deleting all currently saved data and starting afresh
94
+
#'
95
+
#' @description
96
+
#' 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.
97
+
#' @examples
98
+
#' \dontrun{
99
+
#' clear_cache(
100
+
#' dir = "some/subdir",
101
+
#' days = 14,
102
+
#' max_size = 512,
103
+
#' logfile = "some/subdir/logs.txt",
104
+
#' prune_rate = 20L
105
+
#' )
106
+
#' }
107
+
#'
108
+
#' @inheritParams set_cache
109
+
#' @export
54
110
clear_cache<-function(...) {
55
111
cache_environ$epidatr_cache$destroy()
56
112
set_cache(...)
57
113
}
58
114
115
+
#' turn off the caching for this session
116
+
#' @description
117
+
#' 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`.
118
+
#' @export
59
119
disable_cache<-function() {
60
-
cache_environ$epidatr_cache<<-NULL
120
+
cache_environ$epidatr_cache<-NULL
121
+
}
122
+
123
+
#' turn off the caching for this session
124
+
#' @description
125
+
#' Print out the information about the cache (as would be returned by cachem's `info()` method)
126
+
#' @export
127
+
cache_info<-function() {
128
+
cache_environ$epidatr_cache$info()
61
129
}
62
130
131
+
<<<<<<<Updatedupstream
132
+
=======
63
133
cache_epidata_call<-function(call, ...) {
134
+
#' turn off the caching for this session
135
+
#' @description
136
+
#' Print out the information about the cache (as would be returned by cachem's `info()` method)
137
+
#' @export
138
+
cache_info<-function() {
139
+
cache_environ$epidatr_cache$info()
140
+
}
141
+
142
+
>>>>>>>Stashedchanges
143
+
#' create a new cache for this session
144
+
#'
145
+
#' @description
146
+
#' the guts of caching, its interposed between fetch and the specific fetch methods. Internal method only.
147
+
#'
148
+
#' @param call the `epidata_call` object
149
+
#' @inheritParams fetch
150
+
#' @import cachem openssl
64
151
cache_epidata_call<-function(epidata_call, ...) {
65
152
if (cache_environ$use_cache&&!is.null(cache_environ$epidatr_cache)) {
0 commit comments