From d4813679fc26d57a7cc0df9f3244327f87f024a0 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Mon, 23 Jul 2018 15:14:01 -0500 Subject: [PATCH 1/2] implement cache_redis() --- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/cache_redis.R | 55 ++++++++++++++++++++++++++++++++++++++++++++++ man/cache_redis.Rd | 36 ++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 R/cache_redis.R create mode 100644 man/cache_redis.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 8340489..532d00c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,6 +19,7 @@ Suggests: aws.s3, httr, covr, - googleCloudStorageR + googleCloudStorageR, + storr License: MIT + file LICENSE RoxygenNote: 6.0.1 diff --git a/NAMESPACE b/NAMESPACE index eb662d2..9c0125c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ S3method(print,memoised) export(cache_filesystem) export(cache_gcs) export(cache_memory) +export(cache_redis) export(cache_s3) export(forget) export(has_cache) diff --git a/R/cache_redis.R b/R/cache_redis.R new file mode 100644 index 0000000..afff208 --- /dev/null +++ b/R/cache_redis.R @@ -0,0 +1,55 @@ +#' Redis Cache +#' +#' Use a Redis based cache to persist memoisation between R sessions. +#' +#' @param ... arguments passed along to the `hiredis()` function in the **redux** package. +#' @inheritParams cache_memory +#' @author Carson Sievert +#' @export +#' @examples +#' +#' \dontrun{ +#' my_function <- memoise::memoise( +#' function(input) { +#' Sys.sleep(5) +#' paste('Input was', input) +#' }, +#' cache = cache_redis() +#' ) +#' my_function("a") +#' my_function("a") +#' +#' } +cache_redis <- function(..., algo = "md5") { + + if (!requireNamespace("redux")) { + stop("Package `redux` must be installed for `cache_redis()`. Please install and try again.") + } # nocov + + r <- redux::hiredis(...) + + cache_reset <- function() { + r$FLUSHALL() + } + + cache_set <- function(key, value) { + r$SET(key, redux::object_to_bin(value)) + } + + cache_get <- function(key) { + redux::bin_to_object(r$GET(key)) + } + + cache_has_key <- function(key) { + length(r$KEYS(key)) == 1 + } + + list( + digest = function(...) digest::digest(..., algo = algo), + reset = cache_reset, + set = cache_set, + get = cache_get, + has_key = cache_has_key, + keys = function() r$KEYS("*") + ) +} diff --git a/man/cache_redis.Rd b/man/cache_redis.Rd new file mode 100644 index 0000000..58dd6b5 --- /dev/null +++ b/man/cache_redis.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cache_redis.R +\name{cache_redis} +\alias{cache_redis} +\title{Redis Cache} +\usage{ +cache_redis(..., algo = "md5") +} +\arguments{ +\item{...}{arguments passed along to the `hiredis()` function in the **redux** package.} + +\item{algo}{The hashing algorithm used for the cache, see +\code{\link[digest]{digest}} for available algorithms.} +} +\description{ +Use a Redis based cache to persist memoisation between R sessions. +} +\examples{ + +\dontrun{ + +my_function <- memoise::memoise( + function(input) { + Sys.sleep(5) + paste('Input was', input) + }, + cache = cache_redis() +) +my_function("a") +my_function("a") + +} +} +\author{ +Carson Sievert +} From 750db8a5c1b360b88cd8a909a776c7c58ae63a78 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Thu, 26 Jul 2018 14:07:10 -0500 Subject: [PATCH 2/2] redux not storr --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 532d00c..ca2920f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,6 +20,6 @@ Suggests: httr, covr, googleCloudStorageR, - storr + redux License: MIT + file LICENSE RoxygenNote: 6.0.1