gar_cache_setup: Setup where to put cache

Description Usage Arguments Value Examples

View source: R/memoise.R

Description

To cache to a file system use memoise::cache_filesystem("cache_folder"), suitable for unit testing and works between R sessions.

The cached API calls do not need authentication to be active, but need this function to set caching first.

Usage

1
2
3
4
5
6
7
gar_cache_get_loc()

gar_cache_empty()

gar_cache_setup(mcache = memoise::cache_memory(),
  invalid_func = function(req) {     tryCatch(req$status_code == 200, error =
  function(x) FALSE) })

Arguments

mcache

A cache method from memoise.

invalid_func

A function that takes API response, and returns TRUE or FALSE whether caching takes place. Default cache everything.

Value

TRUE if successful.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Not run: 

# demo function to cache within
shorten_url_cache <- function(url){
  body = list(longUrl = url)
  f <- gar_api_generator("https://www.googleapis.com/urlshortener/v1/url",
                      "POST",
                      data_parse_function = function(x) x)
 f(the_body = body)
 
 }
 
 ## only cache if this URL
 gar_cache_setup(invalid_func = function(req){
      req$content$longUrl == "http://code.markedmondson.me/"
 })
 
 # authentication
 gar_auth()
 ## caches
 shorten_url_cache("http://code.markedmondson.me")
 
 ## read cache
 shorten_url("http://code.markedmondson.me")
 
 ## ..but dont cache me
 shorten_url_cache("http://blahblah.com")


## End(Not run)

googleAuthR documentation built on Nov. 17, 2018, 1:04 a.m.