diff --git a/DESCRIPTION b/DESCRIPTION index 8340489..ca2920f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,6 +19,7 @@ Suggests: aws.s3, httr, covr, - googleCloudStorageR + googleCloudStorageR, + redux 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 +}