-
Notifications
You must be signed in to change notification settings - Fork 85
Closed
Labels
bugan unexpected problem or unintended behavioran unexpected problem or unintended behavior
Description
I'm a little confused about if/how caching works when path is specified. Modifying the example in the documentation for req_cache():
library(httr2)
# create cache directory
td = file.path(tempdir(), "cache")
dir.create(td)
url <- paste0(
"https://raw.githubusercontent.com/allisonhorst/palmerpenguins/",
"master/inst/extdata/penguins.csv"
)
# Here I set debug = TRUE so you can see what's happening
req <- request(url) |> req_cache(td, debug = TRUE)
# First request downloads the data
tf <- tempfile(fileext = ".csv")
resp <- req |> req_perform(path = tf)
toString(resp$body)
## [1] "C:\\Temp\\1\\RtmpExocAw\\file53f823441cef.csv"
# Second request retrieves it from the cache
tf2 <- tempfile(fileext = ".csv")
resp <- req |> req_perform(path = tf2)
## Found url in cache "d5d1ddd7f99f55dbc920c63f942804c0"
## Cached value is fresh; retrieving response from cache
toString(resp$body)
## [1] "C:\\Temp\\1\\RtmpExocAw/foo/d5d1ddd7f99f55dbc920c63f942804c0.body"
file.exists(tf2)
## [1] FALSE
# wait a while, cache is now stale
tf3 <- tempfile(fileext = ".csv")
resp <- req |> req_perform(path = tf3)
## Found url in cache "d5d1ddd7f99f55dbc920c63f942804c0"
## Cached value is stale; checking for updates
## Cached value still ok; retrieving body from cache
toString(resp$body)
## [1] "C:\\Temp\\1\\RtmpExocAw\\file53f815c51993.csv"
file.exists(tf3)
## [1] TRUEWhen the cached value is fresh, the response returns a different path, which makes sense since there is no guarantee that the path specified in the original call still exists when the request is made a second time. My questions are:
- Is the file downloaded once and written to both the
pathand cache? (Appears to be the case). - Is there a way to tell the cache to use the same file extension as specified in
path? I can see this possibly being an issue for some functions that expect a certain file extension. - If the cache is stale, it appears to re-download the file. If it decides the cached value is still ok, it claims to retrieve the body from cache but actually provides
path. It seems to do this for every subsequent request, i.e., the cache is not "refreshed" and it continues to think the cache is stale. Is this a bug?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugan unexpected problem or unintended behavioran unexpected problem or unintended behavior