redis_loadConfig: Load Config-Files to Redis Cache

Description Usage Arguments Value Data Format Auto-Load References Examples

View source: R/loadConfig.R

Description

The function loads config data, stored as JSON-strings in config files to the Redis cache.

Usage

1
2
3
4
5

Arguments

file

character vector of file names (a single file name for redis_readConfigFile)

append

logical, if TRUE config settings will be appended to existing settings (if any). Duplicate settings will be overwritten.

expr

character string, expression to evaluate, see Auto-Load section for details

parent

environment, parent environment for the environment in which expr will be evaluated

Value

a list of configuration settings

Data Format

All config files must contain data in JSON format. Basic elements of the data structure at top level are

key

mandatory, unique key to store and access data in the Redis cache

value

mandatory, data object containing a list of key-value pairs. This can be any list of named data. There is one mandatory element: id. This is a unique identifier of the data. It will be matched to key and no key must be assigned to one or more ids (e.g. from multiple config-files)

auto_load

optional, an auto load obejct, i.e. a list of key-value pairs, see Auto Load section for details.

For loading a minimal config file, see Examples.

Auto-Load

The auto_load section of the config settings contains a named list of R expressions, e.g.

1
2
3
"auto_load": {
    "date": "Sys.Date()"
}

will set the config value date to the current date (using Sys.Date()). Use "::" to preface the function with the package name (just like in plain R). A single colon : can be used to preface the expression with a file name (i.e. anything that ends with ".R" or ".r"), that will be sourced before the expression will be evaluated.

If file name is provided without any expression, variable value as defined by the script will be returned.

All expressions (and the sourcing of the R-script, in case a file name is provided) will be evaluated in an separate environment with parent environment parent.

Function redis_readConfigFile, that calls .auto_load will set parent to a new environment containing all non-auto-load config settings. The parent environment of this is set to .GlobalEnv. Thus all config settings and any object within the global search path will be available for execution of expr.

Function redis_readConfigFile further sets the working directory temporarily to the config file's base directory, i.e. all relative paths in a file name will be with respect to this directory.

References

see https://en.wikipedia.org/wiki/JSON for details on JSON file format

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
## Not run: 
## show template for minimal configuration file
file <- system.file('templates/minimal.cfg', package = 'easyRedis')
cat(stringr::str_c(readLines(file), collapse = '\n'))

## *** SET UP AN OPEN REDIS-CONNECTION FIRST! ***
config <- redis_loadConfig(file)

key <- attr(config, 'key')

redis_getConfig(key)

## *** CLEAR REDIS KEY! ***
rredis::redisDelete(key)

## End(Not run)

mlkornexl/easyRedis documentation built on May 17, 2019, 9:13 a.m.