Initlaize storage for name value pairs based on provided type.

Available types of storage include:

  • memory: transient in-memory storage

  • file: persistent storage, using local file storage

  • object: provider-backed object storage

Use file when you want a simple local file store without provider configuration. Use object with a provider (e.g. file, azure.blob) when you want a consistent, pluggable interface across storage backends. This allows the same code path to swap between local and cloud providers.

Additional paramters may be provided when initializing different types of storage. If storage.type is not provided and memofunc.storage.provider is set, then the provider is used to initialize storage.

Providers are resolved by name (for example file or azure.blob), with synonyms such as local or azure mapping to their canonical names. The Azure provider requires the AzureStor package.

See specific storage types for details.

storage.init(
  storage.type = storage.memory.class,
  provider = getOption("memofunc.storage.provider", NULL),
  ...
)

Arguments

storage.type

storage type to initialize, defaults to memory

provider

optional provider name or provider configuration

...

additional configuration values used by storage implementations

Value

List containing characteristics perticular to the storage implementation, including:

  • type: the storage type field

Examples

library(magrittr)

# initialize default memory storage
my.storage <- storage.init()

# set a value into storage
storage.set(my.storage, "name", "Roy Wetherall")

# .. and some more
my.storage %>% 
  storage.set("age", 45) %>% 
  storage.set("alive", TRUE) %>%
  storage.set("children", c("Peter", "Grace", "Lucy"))

# check a key has been set
if (storage.has(my.storage, "name")) print("I know your name!")
#> [1] "I know your name!"

# .. and that a key hasn't been set
if (!storage.has(my.storage, "address")) print("I don't know where you live!")
#> [1] "I don't know where you live!"

# get some values from storage
sprintf(
  "%s is %i years old.", 
  storage.get(my.storage, "name"),
  storage.get(my.storage, "age"))
#> [1] "Roy Wetherall is 45 years old."

# remove a value from storage
storage.unset(my.storage, "children")

# .. and show it's not there anymore
if (!storage.has(my.storage, "address")) print("I don't know who your children are!")
#> [1] "I don't know who your children are!"

# clear all values from storage
storage.clear(my.storage)

# .. and everything is gone
if (!storage.has(my.storage, "name") && !storage.has(my.storage, "age")) print("I know nothing!")
#> [1] "I know nothing!"

# initialize file storage (direct backend; simplest local-only path)
base.dir <- file.path(tempdir(), "memofunc-storage")
file.storage <- storage.init("file", base.dir = base.dir)

# set and retrieve a value from file storage (direct backend)
storage.set(file.storage, "name", "Roy Wetherall")
storage.get(file.storage, "name")
#> [1] "Roy Wetherall"

# initialize object storage using a provider name (provider-backed interface)
# this uses the same local file backend now, but lets you swap to azure.blob/s3 later
object.storage <- storage.init("object", provider = "file", base.dir = base.dir)
storage.set(object.storage, "name", "Roy Wetherall")
storage.get(object.storage, "name")
#> [1] "Roy Wetherall"

# set a default provider via options
old.options <- options(memofunc.storage.provider = list(name = "file", base.dir = base.dir))
on.exit(options(old.options), add = TRUE)
default.storage <- storage.init()
storage.get(default.storage, "name")
#> NULL

# Azure Blob provider example (requires AzureStor + credentials)
if (requireNamespace("AzureStor", quietly = TRUE)) {
  account <- Sys.getenv("AZURE_STORAGE_ACCOUNT")
  container <- Sys.getenv("AZURE_STORAGE_CONTAINER")
  key <- Sys.getenv("AZURE_STORAGE_KEY")
  token <- Sys.getenv("AZURE_STORAGE_TOKEN")

  if (account != "" && container != "" && (key != "" || token != "")) {
    azure.storage <- storage.init(
      "object",
      provider = "azure.blob",
      account = account,
      container = container,
      key = if (key == "") NULL else key,
      token = if (token == "") NULL else token,
      prefix = "memofunc-example"
    )

    storage.set(azure.storage, "name", "Roy Wetherall")
    storage.get(azure.storage, "name")
  }
}