From 829a6b4f7461aaefda4b124e2dadf9d250835e8b Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Wed, 20 Jan 2021 07:29:31 +1100 Subject: [PATCH 1/7] add Azure storage option --- DESCRIPTION | 2 + NAMESPACE | 1 + R/cache_azure.R | 98 +++++++++++++++++++++++++++++++++++++ R/cache_filesystem.R | 6 +++ man/cache_azure.Rd | 57 +++++++++++++++++++++ man/cache_filesystem.Rd | 6 +++ tests/testthat/helper.R | 11 +++++ tests/testthat/test-azure.R | 40 +++++++++++++++ 8 files changed, 221 insertions(+) create mode 100644 R/cache_azure.R create mode 100644 man/cache_azure.Rd create mode 100644 tests/testthat/test-azure.R diff --git a/DESCRIPTION b/DESCRIPTION index a38eb60..408dd66 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,6 +41,8 @@ Suggests: covr, googleAuthR, googleCloudStorageR, + AzureStor (>= 3.0.0), + AzureAuth, httr, testthat Remotes: diff --git a/NAMESPACE b/NAMESPACE index 82acdd3..1acdd7b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand S3method(print,memoised) +export(cache_azure) export(cache_filesystem) export(cache_gcs) export(cache_memory) diff --git a/R/cache_azure.R b/R/cache_azure.R new file mode 100644 index 0000000..642b380 --- /dev/null +++ b/R/cache_azure.R @@ -0,0 +1,98 @@ +#' Azure Storage Cache +#' Azure Storage backed cache, for remote caching. You can use either file, blob or ADLSgen2 storage. +#' +#' @examples +#' +#' \dontrun{ +#' library(AzureStor) +#' +#' stor <- storage_container("https://blob.{accountname}.core.windows.net", +#' key="{storage-key}") +#' +#' azcache <- cache_azure("cache_container", stor) +#' mem_runif <- memoise(runif, cache = azcache) +#' +#' +#' # alternative way of specifying the account +#' azcache <- cache_azure("cache_container", "https://blob.{accountname}.core.windows.net", +#' key="{storage_key}") +#' } +#' +#' +#' @param cache_name Name of the storage container for storing cache files. +#' @param account The storage account for the cache. This should be an object of class \code{\link[AzureStor::storage_endpoint]{AzureStor::storage_endpoint}}, or inheriting from it. Alternatively, you can provide the storage endpoint URL, along with one of the authentication arguments \code{key}, \code{token} or \code{sas}. +#' @param key The access key for the storage account. +#' @param token An Azure Active Directory (AAD) authentication token. This can be either a string, or an object of class \code{AzureToken} created by \code{\link[AzureAuth::get_azure_token]{AzureAuth::get_azure_token}}. The latter is the recommended way of doing it, as it allows for automatic refreshing of expired tokens. +#' @param sas A shared access signature (SAS) for the account. +#' @param compress Argument passed to \code{saveRDS}. One of FALSE, "gzip", +#' "bzip2" or "xz". Default: FALSE. +#' @inheritParams cache_memory +#' @export +cache_azure <- function(cache_name, account, key = NULL, token = NULL, sas = NULL, + algo = "sha512", compress = FALSE) { + if(!requireNamespace("AzureStor")) { stop("Package `AzureStor` must be installed for `cache_azure()`.") } # nocov + + if(is.character(account)) { + storage_account <- AzureStor::storage_endpoint(account, key, sas, token) + } else if(!inherits(account, "storage_endpoint")) { + stop("Must provide either the URL of a storage account endpoint, or a `storage_endpoint` object") + } + + path <- tempfile("memoise-") + dir.create(path) + + # create container if it doesn't exist + try(AzureStor::create_storage_container(account, cache_name), silent = TRUE) + cache <- AzureStor::storage_container(account, cache_name) + + cache_reset <- function() { + keys <- cache_keys() + lapply(keys, function(key) { + AzureStor::delete_storage_file(cache, key, confirm = FALSE) + }) + } + + cache_set <- function(key, value) { + rds <- file.path(path, key) + saveRDS(value, rds, compress = compress) + opts <- options(azure_storage_progress_bar = FALSE) + on.exit({ + unlink(rds) + options(opts) + }) + AzureStor::storage_upload(cache, rds, key) + } + + cache_get <- function(key) { + rds <- file.path(path, key) + opts <- options(azure_storage_progress_bar = FALSE) + on.exit({ + unlink(rds) + options(opts) + }) + AzureStor::storage_download(cache, key, rds, overwrite = TRUE) + readRDS(rds) + } + + cache_has_key <- function(key) { + key %in% cache_keys() + } + + cache_drop_key <- function(key) { + AzureStor::delete_storage_file(cache, key, confirm = FALSE) + } + + cache_keys <- function() { + AzureStor::list_storage_files(cache, info = "name") + } + + list( + digest = function(...) digest::digest(..., algo = algo), + reset = cache_reset, + set = cache_set, + get = cache_get, + has_key = cache_has_key, + drop_key = cache_drop_key, + keys = cache_keys + ) +} diff --git a/R/cache_filesystem.R b/R/cache_filesystem.R index 153f8d4..aaef39a 100644 --- a/R/cache_filesystem.R +++ b/R/cache_filesystem.R @@ -21,6 +21,12 @@ #' #' mem_runif <- memoise(runif, cache = gd) #' +#' # Use with OneDrive (on Windows) +#' +#' od <- cache_filesystem("~/../OneDrive") +#' +#' mem_runif <- memoise(runif, cache = od) +#' #' } #' #' @export diff --git a/man/cache_azure.Rd b/man/cache_azure.Rd new file mode 100644 index 0000000..12e460b --- /dev/null +++ b/man/cache_azure.Rd @@ -0,0 +1,57 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cache_azure.R +\name{cache_azure} +\alias{cache_azure} +\title{Azure Storage Cache +Azure Storage backed cache, for remote caching. You can use either file, blob or ADLSgen2 storage.} +\usage{ +cache_azure( + cache_name, + account, + key = NULL, + token = NULL, + sas = NULL, + algo = "sha512", + compress = FALSE +) +} +\arguments{ +\item{cache_name}{Name of the storage container for storing cache files.} + +\item{account}{The storage account for the cache. This should be an object of class \code{\link[AzureStor::storage_endpoint]{AzureStor::storage_endpoint}}, or inheriting from it. Alternatively, you can provide the storage endpoint URL, along with one of the authentication arguments \code{key}, \code{token} or \code{sas}.} + +\item{key}{The access key for the storage account.} + +\item{token}{An Azure Active Directory (AAD) authentication token. This can be either a string, or an object of class \code{AzureToken} created by \code{\link[AzureAuth::get_azure_token]{AzureAuth::get_azure_token}}. The latter is the recommended way of doing it, as it allows for automatic refreshing of expired tokens.} + +\item{sas}{A shared access signature (SAS) for the account.} + +\item{algo}{The hashing algorithm used for the cache, see +\code{\link[digest]{digest}} for available algorithms.} + +\item{compress}{Argument passed to \code{saveRDS}. One of FALSE, "gzip", +"bzip2" or "xz". Default: FALSE.} +} +\description{ +Azure Storage Cache +Azure Storage backed cache, for remote caching. You can use either file, blob or ADLSgen2 storage. +} +\examples{ + +\dontrun{ +library(AzureStor) + +stor <- storage_container("https://blob.{accountname}.core.windows.net", + key="{storage-key}") + +azcache <- cache_azure("cache_container", stor) +mem_runif <- memoise(runif, cache = azcache) + + +# alternative way of specifying the account +azcache <- cache_azure("cache_container", "https://blob.{accountname}.core.windows.net", + key="{storage_key}") +} + + +} diff --git a/man/cache_filesystem.Rd b/man/cache_filesystem.Rd index a2c678b..b9981fa 100644 --- a/man/cache_filesystem.Rd +++ b/man/cache_filesystem.Rd @@ -33,6 +33,12 @@ gd <- cache_filesystem("~/Google Drive/.rcache") mem_runif <- memoise(runif, cache = gd) +# Use with OneDrive (on Windows) + +od <- cache_filesystem("~/../OneDrive") + +mem_runif <- memoise(runif, cache = od) + } } diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index af8164c..006a624 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -23,3 +23,14 @@ skip_on_travis_pr <- function() { invisible(TRUE) } + +skip_without_azure_credentials <- function() { + storage_url <- Sys.getenv("AZ_TEST_STORAGE_MEMOISE_URL") + storage_key <- Sys.getenv("AZ_TEST_STORAGE_MEMOISE_KEY") + + if (storage_url == "" || storage_key == "") { + testthat::skip("No Azure storage credentials") + } else { + invisible(TRUE) + } +} diff --git a/tests/testthat/test-azure.R b/tests/testthat/test-azure.R new file mode 100644 index 0000000..6d9cfdb --- /dev/null +++ b/tests/testthat/test-azure.R @@ -0,0 +1,40 @@ +context("azure storage") + +skip_on_cran() +skip_on_travis_pr() +skip_without_azure_credentials() + +storage_url <- Sys.getenv("AZ_TEST_STORAGE_MEMOISE_URL") +storage_key <- Sys.getenv("AZ_TEST_STORAGE_MEMOISE_KEY") + +bl_endp <- AzureStor::storage_endpoint(storage_url, key=storage_key) + +test_that("using an Azure storage cache works", { + + azcache <- cache_azure("memoise-tests", bl_endp) + i <- 0 + fn <- function() { i <<- i + 1; i } + fnm <- memoise(fn, cache = azcache) + on.exit(forget(fnm)) + + expect_equal(fn(), 1) + expect_equal(fn(), 2) + expect_equal(fnm(), 3) + expect_equal(fnm(), 3) + expect_equal(fn(), 4) + expect_equal(fnm(), 3) + + expect_false(forget(fn)) + expect_true(forget(fnm)) + expect_equal(fnm(), 5) + + expect_true(drop_cache(fnm)()) + expect_equal(fnm(), 6) + + expect_true(is.memoised(fnm)) + expect_false(is.memoised(fn)) +}) + +teardown({ + AzureStor::delete_blob_container(bl_endp, "memoise-tests", confirm=FALSE) +}) From 02e51a6515abef40ee374583a1cd787e733778f7 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Wed, 20 Jan 2021 07:52:25 +1100 Subject: [PATCH 2/7] don't link to suggested pkg docs --- R/cache_azure.R | 4 ++-- man/cache_azure.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/cache_azure.R b/R/cache_azure.R index 642b380..fc72752 100644 --- a/R/cache_azure.R +++ b/R/cache_azure.R @@ -20,9 +20,9 @@ #' #' #' @param cache_name Name of the storage container for storing cache files. -#' @param account The storage account for the cache. This should be an object of class \code{\link[AzureStor::storage_endpoint]{AzureStor::storage_endpoint}}, or inheriting from it. Alternatively, you can provide the storage endpoint URL, along with one of the authentication arguments \code{key}, \code{token} or \code{sas}. +#' @param account The storage account for the cache. This should be an object of class \code{AzureStor::storage_endpoint}, or inheriting from it. Alternatively, you can provide the storage endpoint URL, along with one of the authentication arguments \code{key}, \code{token} or \code{sas}. #' @param key The access key for the storage account. -#' @param token An Azure Active Directory (AAD) authentication token. This can be either a string, or an object of class \code{AzureToken} created by \code{\link[AzureAuth::get_azure_token]{AzureAuth::get_azure_token}}. The latter is the recommended way of doing it, as it allows for automatic refreshing of expired tokens. +#' @param token An Azure Active Directory (AAD) authentication token. This can be either a string, or an object of class \code{AzureToken} created by \code{AzureAuth::get_azure_token}. The latter is the recommended way of doing it, as it allows for automatic refreshing of expired tokens. #' @param sas A shared access signature (SAS) for the account. #' @param compress Argument passed to \code{saveRDS}. One of FALSE, "gzip", #' "bzip2" or "xz". Default: FALSE. diff --git a/man/cache_azure.Rd b/man/cache_azure.Rd index 12e460b..1cc7ea2 100644 --- a/man/cache_azure.Rd +++ b/man/cache_azure.Rd @@ -18,11 +18,11 @@ cache_azure( \arguments{ \item{cache_name}{Name of the storage container for storing cache files.} -\item{account}{The storage account for the cache. This should be an object of class \code{\link[AzureStor::storage_endpoint]{AzureStor::storage_endpoint}}, or inheriting from it. Alternatively, you can provide the storage endpoint URL, along with one of the authentication arguments \code{key}, \code{token} or \code{sas}.} +\item{account}{The storage account for the cache. This should be an object of class \code{AzureStor::storage_endpoint}, or inheriting from it. Alternatively, you can provide the storage endpoint URL, along with one of the authentication arguments \code{key}, \code{token} or \code{sas}.} \item{key}{The access key for the storage account.} -\item{token}{An Azure Active Directory (AAD) authentication token. This can be either a string, or an object of class \code{AzureToken} created by \code{\link[AzureAuth::get_azure_token]{AzureAuth::get_azure_token}}. The latter is the recommended way of doing it, as it allows for automatic refreshing of expired tokens.} +\item{token}{An Azure Active Directory (AAD) authentication token. This can be either a string, or an object of class \code{AzureToken} created by \code{AzureAuth::get_azure_token}. The latter is the recommended way of doing it, as it allows for automatic refreshing of expired tokens.} \item{sas}{A shared access signature (SAS) for the account.} From f11ed767ffffe0282922bbe200128918924ddd94 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Wed, 20 Jan 2021 08:19:12 +1100 Subject: [PATCH 3/7] use new-style cache --- R/cache_azure.R | 10 ++++++---- tests/testthat/test-azure.R | 5 +++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/R/cache_azure.R b/R/cache_azure.R index fc72752..f463551 100644 --- a/R/cache_azure.R +++ b/R/cache_azure.R @@ -70,7 +70,10 @@ cache_azure <- function(cache_name, account, key = NULL, token = NULL, sas = NUL unlink(rds) options(opts) }) - AzureStor::storage_download(cache, key, rds, overwrite = TRUE) + res <- try(AzureStor::storage_download(cache, key, rds, overwrite = TRUE), silent = TRUE) + if(inherits(res, "try-error")) { + return(cachem::key_missing()) + } readRDS(rds) } @@ -87,12 +90,11 @@ cache_azure <- function(cache_name, account, key = NULL, token = NULL, sas = NUL } list( - digest = function(...) digest::digest(..., algo = algo), reset = cache_reset, set = cache_set, get = cache_get, - has_key = cache_has_key, - drop_key = cache_drop_key, + exists = cache_has_key, + remove = cache_drop_key, keys = cache_keys ) } diff --git a/tests/testthat/test-azure.R b/tests/testthat/test-azure.R index 6d9cfdb..b270057 100644 --- a/tests/testthat/test-azure.R +++ b/tests/testthat/test-azure.R @@ -8,10 +8,11 @@ storage_url <- Sys.getenv("AZ_TEST_STORAGE_MEMOISE_URL") storage_key <- Sys.getenv("AZ_TEST_STORAGE_MEMOISE_KEY") bl_endp <- AzureStor::storage_endpoint(storage_url, key=storage_key) +cache_name <- paste0(sample(letters, 20, TRUE), collapse = "") test_that("using an Azure storage cache works", { - azcache <- cache_azure("memoise-tests", bl_endp) + azcache <- cache_azure(cache_name, bl_endp) i <- 0 fn <- function() { i <<- i + 1; i } fnm <- memoise(fn, cache = azcache) @@ -36,5 +37,5 @@ test_that("using an Azure storage cache works", { }) teardown({ - AzureStor::delete_blob_container(bl_endp, "memoise-tests", confirm=FALSE) + AzureStor::delete_blob_container(bl_endp, cache_name, confirm = FALSE) }) From 7921b397ca53d17d42dcb063a9ba8b3a2314197c Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Wed, 20 Jan 2021 08:19:56 +1100 Subject: [PATCH 4/7] update list of authors --- DESCRIPTION | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 408dd66..d8fe672 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,7 +25,11 @@ Authors@R: person(given = "Mark", family = "Edmondson", role = "ctb", - email = "r@sunholo.com")) + email = "r@sunholo.com"), + person(given = "Hong", + family = "Ooi", + role = "ctb", + email = "hongooi73@gmail.com")) Description: Cache the results of a function so that when you call it again with the same arguments it returns the pre-computed value. From 6be4ac3fc310fdec753194f8e82bd6c21f586c43 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Wed, 20 Jan 2021 08:26:09 +1100 Subject: [PATCH 5/7] fix order of args in storage_endpoint call --- R/cache_azure.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/cache_azure.R b/R/cache_azure.R index f463551..84c4a3b 100644 --- a/R/cache_azure.R +++ b/R/cache_azure.R @@ -33,7 +33,7 @@ cache_azure <- function(cache_name, account, key = NULL, token = NULL, sas = NUL if(!requireNamespace("AzureStor")) { stop("Package `AzureStor` must be installed for `cache_azure()`.") } # nocov if(is.character(account)) { - storage_account <- AzureStor::storage_endpoint(account, key, sas, token) + storage_account <- AzureStor::storage_endpoint(account, key, token, sas) } else if(!inherits(account, "storage_endpoint")) { stop("Must provide either the URL of a storage account endpoint, or a `storage_endpoint` object") } From 43542037825765b8f9c92b57367a54bd750c0efc Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Wed, 20 Jan 2021 09:26:26 +1100 Subject: [PATCH 6/7] Update cache_azure.R --- R/cache_azure.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/cache_azure.R b/R/cache_azure.R index 84c4a3b..4378ab8 100644 --- a/R/cache_azure.R +++ b/R/cache_azure.R @@ -33,7 +33,7 @@ cache_azure <- function(cache_name, account, key = NULL, token = NULL, sas = NUL if(!requireNamespace("AzureStor")) { stop("Package `AzureStor` must be installed for `cache_azure()`.") } # nocov if(is.character(account)) { - storage_account <- AzureStor::storage_endpoint(account, key, token, sas) + account <- AzureStor::storage_endpoint(account, key, token, sas) } else if(!inherits(account, "storage_endpoint")) { stop("Must provide either the URL of a storage account endpoint, or a `storage_endpoint` object") } From fde8538da64feccd61ba74715e07dfa4add32700 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Wed, 20 Jan 2021 10:42:02 +1100 Subject: [PATCH 7/7] improve interface --- R/cache_azure.R | 44 +++++++++++++++++++++------------------- man/cache_azure.Rd | 50 +++++++++++++++++++--------------------------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/R/cache_azure.R b/R/cache_azure.R index 4378ab8..30f09c7 100644 --- a/R/cache_azure.R +++ b/R/cache_azure.R @@ -1,40 +1,44 @@ #' Azure Storage Cache -#' Azure Storage backed cache, for remote caching. You can use either file, blob or ADLSgen2 storage. +#' +#' Azure Storage backed cache, for remote caching. File, blob and ADLSgen2 storage are all supported. #' #' @examples #' #' \dontrun{ #' library(AzureStor) #' -#' stor <- storage_container("https://blob.{accountname}.core.windows.net", -#' key="{storage-key}") -#' +#' # use Azure blob storage for the cache +#' stor <- storage_endpoint("https://blob.accountname.core.windows.net", key="storage-key") #' azcache <- cache_azure("cache_container", stor) #' mem_runif <- memoise(runif, cache = azcache) #' #' -#' # alternative way of specifying the account -#' azcache <- cache_azure("cache_container", "https://blob.{accountname}.core.windows.net", -#' key="{storage_key}") -#' } +#' # you can also pass the endpoint URL and key to cache_azure: +#' azcache <- cache_azure("cache_container", "https://blob.accountname.core.windows.net", +#' key = "storage-key") #' +#' # a better alternative to a master key: OAuth 2.0 authentication via AAD +#' token <- AzureAuth::get_azure_token( +#' "https://storage.azure.com", +#' tenant = "mytenant", +#' app = "app_id" +#' ) +#' azcache <- cache_azure("cache_container", "https://blob.accountname.core.windows.net", +#' token = token) +#' } #' #' @param cache_name Name of the storage container for storing cache files. -#' @param account The storage account for the cache. This should be an object of class \code{AzureStor::storage_endpoint}, or inheriting from it. Alternatively, you can provide the storage endpoint URL, along with one of the authentication arguments \code{key}, \code{token} or \code{sas}. -#' @param key The access key for the storage account. -#' @param token An Azure Active Directory (AAD) authentication token. This can be either a string, or an object of class \code{AzureToken} created by \code{AzureAuth::get_azure_token}. The latter is the recommended way of doing it, as it allows for automatic refreshing of expired tokens. -#' @param sas A shared access signature (SAS) for the account. +#' @param endpoint The storage account endpoint for the cache. This should be an object of class \code{AzureStor::storage_endpoint}, or inheriting from it. Alternatively, you can provide the endpoint URL as a string, and pass any authentication arguments in `...`. #' @param compress Argument passed to \code{saveRDS}. One of FALSE, "gzip", #' "bzip2" or "xz". Default: FALSE. -#' @inheritParams cache_memory +#' @param ... Further arguments that will be passed to \code{AzureStor::storage_endpoint}, if \code{endpoint} is a URL. #' @export -cache_azure <- function(cache_name, account, key = NULL, token = NULL, sas = NULL, - algo = "sha512", compress = FALSE) { +cache_azure <- function(cache_name, endpoint, compress = FALSE, ...) { if(!requireNamespace("AzureStor")) { stop("Package `AzureStor` must be installed for `cache_azure()`.") } # nocov - if(is.character(account)) { - account <- AzureStor::storage_endpoint(account, key, token, sas) - } else if(!inherits(account, "storage_endpoint")) { + if(is.character(endpoint)) { + endpoint <- AzureStor::storage_endpoint(endpoint, ...) + } else if(!inherits(endpoint, "storage_endpoint")) { stop("Must provide either the URL of a storage account endpoint, or a `storage_endpoint` object") } @@ -42,8 +46,8 @@ cache_azure <- function(cache_name, account, key = NULL, token = NULL, sas = NUL dir.create(path) # create container if it doesn't exist - try(AzureStor::create_storage_container(account, cache_name), silent = TRUE) - cache <- AzureStor::storage_container(account, cache_name) + try(AzureStor::create_storage_container(endpoint, cache_name), silent = TRUE) + cache <- AzureStor::storage_container(endpoint, cache_name) cache_reset <- function() { keys <- cache_keys() diff --git a/man/cache_azure.Rd b/man/cache_azure.Rd index 1cc7ea2..d19686a 100644 --- a/man/cache_azure.Rd +++ b/man/cache_azure.Rd @@ -2,56 +2,46 @@ % Please edit documentation in R/cache_azure.R \name{cache_azure} \alias{cache_azure} -\title{Azure Storage Cache -Azure Storage backed cache, for remote caching. You can use either file, blob or ADLSgen2 storage.} +\title{Azure Storage Cache} \usage{ -cache_azure( - cache_name, - account, - key = NULL, - token = NULL, - sas = NULL, - algo = "sha512", - compress = FALSE -) +cache_azure(cache_name, endpoint, compress = FALSE, ...) } \arguments{ \item{cache_name}{Name of the storage container for storing cache files.} -\item{account}{The storage account for the cache. This should be an object of class \code{AzureStor::storage_endpoint}, or inheriting from it. Alternatively, you can provide the storage endpoint URL, along with one of the authentication arguments \code{key}, \code{token} or \code{sas}.} - -\item{key}{The access key for the storage account.} - -\item{token}{An Azure Active Directory (AAD) authentication token. This can be either a string, or an object of class \code{AzureToken} created by \code{AzureAuth::get_azure_token}. The latter is the recommended way of doing it, as it allows for automatic refreshing of expired tokens.} - -\item{sas}{A shared access signature (SAS) for the account.} - -\item{algo}{The hashing algorithm used for the cache, see -\code{\link[digest]{digest}} for available algorithms.} +\item{endpoint}{The storage account endpoint for the cache. This should be an object of class \code{AzureStor::storage_endpoint}, or inheriting from it. Alternatively, you can provide the endpoint URL as a string, and pass any authentication arguments in `...`.} \item{compress}{Argument passed to \code{saveRDS}. One of FALSE, "gzip", "bzip2" or "xz". Default: FALSE.} + +\item{...}{Further arguments that will be passed to \code{AzureStor::storage_endpoint}, if \code{endpoint} is a URL.} } \description{ -Azure Storage Cache -Azure Storage backed cache, for remote caching. You can use either file, blob or ADLSgen2 storage. +Azure Storage backed cache, for remote caching. File, blob and ADLSgen2 storage are all supported. } \examples{ \dontrun{ library(AzureStor) -stor <- storage_container("https://blob.{accountname}.core.windows.net", - key="{storage-key}") - +# use Azure blob storage for the cache +stor <- storage_endpoint("https://blob.accountname.core.windows.net", key="storage-key") azcache <- cache_azure("cache_container", stor) mem_runif <- memoise(runif, cache = azcache) -# alternative way of specifying the account -azcache <- cache_azure("cache_container", "https://blob.{accountname}.core.windows.net", - key="{storage_key}") -} +# you can also pass the endpoint URL and key to cache_azure: +azcache <- cache_azure("cache_container", "https://blob.accountname.core.windows.net", + key = "storage-key") +# a better alternative to a master key: OAuth 2.0 authentication via AAD +token <- AzureAuth::get_azure_token( + "https://storage.azure.com", + tenant = "mytenant", + app = "app_id" +) +azcache <- cache_azure("cache_container", "https://blob.accountname.core.windows.net", + token = token) +} }