Skip to content

Commit 015921c

Browse files
authored
Only retrieve GET quieries from cache (#573)
1 parent f4dc72d commit 015921c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# httr2 1.0.5
44

5+
* `req_cache()` no longer retrieves anything but `GET` requests from the cache.
56
* New `resp_stream_aws()` to retrieve AWS's special streaming format. With thanks to <https://github.com/lifion/lifion-aws-event-stream/> for a simple reference implementation.
67
* New `req_auth_aws_v4()` signs request using AWS's special format (#562, #566).
78
* `req_perform_parallel()` and `req_perform_promise()` now correctly set up the method and body (#549).

R/req-cache.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ cache_pre_fetch <- function(req, path = NULL) {
186186
return(req)
187187
}
188188

189+
# Only GET requests should be retrieved from cache. It's not sufficient to
190+
# only save GET requests, because the method is not part of the cache key
191+
if (req_method_get(req) != "GET") {
192+
return(req)
193+
}
194+
189195
debug <- cache_debug(req)
190196
cache_prune_if_needed(req, debug = debug)
191197

tests/testthat/test-req-cache.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ test_that("nothing happens if cache not enabled", {
66
expect_equal(cache_post_fetch(req, resp), resp)
77
})
88

9+
test_that("never retrieves POST request from cache", {
10+
req <- request("http://example.com") %>%
11+
req_method("POST") %>%
12+
req_cache(tempfile())
13+
14+
# Fake an equivalent GET request in the cache
15+
resp <- response(200,
16+
headers = "Expires: Wed, 01 Jan 3000 00:00:00 GMT",
17+
body = charToRaw("abc")
18+
)
19+
cache_set(req, resp)
20+
21+
expect_equal(cache_pre_fetch(req), req)
22+
})
23+
924
test_that("immutable objects retrieved directly from cache", {
1025
req <- request("http://example.com") %>% req_cache(tempfile())
1126
resp <- response(200,

0 commit comments

Comments
 (0)