Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Suggests:
aws.s3,
httr,
covr,
googleCloudStorageR
googleCloudStorageR,
redux
License: MIT + file LICENSE
RoxygenNote: 6.0.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions R/cache_redis.R
Original file line number Diff line number Diff line change
@@ -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("*")
)
}
36 changes: 36 additions & 0 deletions man/cache_redis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.