R/cache.R

Defines functions ga_cache_call

Documented in ga_cache_call

#' Setup caching of API calls
#' 
#' Lets you cache API calls to disk
#' 
#' @param cache_location If `RAM` will save to memory, or specify a file folder location
#' 
#' @details 
#' 
#' By default this is turned on upon package load to `RAM`.  
#'   Should you want to cache calls to a folder then run this function to specify where.
#' 
#' @export
#' @importFrom googleAuthR gar_cache_setup
#' @import assertthat
#' @importFrom memoise cache_filesystem
ga_cache_call <- function(cache_location){
  
  if(cache_location == "RAM"){
    return(gar_cache_setup())
  }
  
  assert_that(
    is.writeable(cache_location)
  )
  
  f <- function(req){
    
    ga4 <- tryCatch(req$content$reports, error = function(x) NULL)
    data <-tryCatch(req$content$rows, error = function(x) NULL)
    
    if(!is.null(ga4) || !is.null(data)) return(TRUE)
    
    myMessage("No caching", level = 2)
    FALSE
    
    }
  
  
  gar_cache_setup(mcache = memoise::cache_filesystem(cache_location),
                  invalid_func = f)
  
}

Try the googleAnalyticsR package in your browser

Any scripts or data that you put into this service are public.

googleAnalyticsR documentation built on Oct. 16, 2022, 1:06 a.m.