Skip to contents

Returns the same client with the cache attached, so a cached mock client is still a mock client and $calls() still records only the calls that were issued, which is how you check that the cache is working.

Usage

gr_cache_client(client, cache = gr_cache())

Arguments

client

A gr_client() or gr_mock_client().

cache

A gr_cache(). Pass NULL to detach.

Value

The client, with the cache attached.

Examples

calls <- 0
cl <- gr_mock_client(function(m, p) { calls <<- calls + 1; "42" })
cl <- gr_cache_client(cl, gr_cache(file.path(tempdir(), "readgpt-doc-cache")))

gr_call(cl, "What is the answer?")$text
#> [1] "42"
gr_call(cl, "What is the answer?")$text   # served from the cache
#> [1] "42"
calls                                     # the handler ran once
#> [1] 1

# A cache hit is marked, so a trace can tell spending from replay.
gr_call(cl, "What is the answer?")$cached
#> [1] TRUE