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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export(hitCache)
export(loadCache)
export(loadLogfile)
export(logMessage)
export(registerCache)
export(requestLogSummary)
export(saveCache)
export(setCache)
Expand Down
16 changes: 9 additions & 7 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cacheOff <- function () {
#' @export
clearCache <- function () {
logMessage("CACHE CLEAR")
rm(list=cacheKeys(), envir=cache)
rm(list=cacheKeys(), envir = identifyCache())
}

#' HTTP Cache API
Expand All @@ -37,15 +37,15 @@ clearCache <- function () {
#' @name cache-api
#' @export
hitCache <- function (key) {
exists(key, envir=cache)
exists(key, envir = identifyCache())
}

#' @rdname cache-api
#' @export
getCache <- function (key) {
if (hitCache(key)) {
logMessage("CACHE HIT", key)
return(get(key, envir=cache))
return(get(key, envir = identifyCache()))
} else {
return(NULL)
}
Expand All @@ -55,10 +55,12 @@ getCache <- function (key) {
#' @export
setCache <- function (key, value) {
logMessage("CACHE SET", key)
assign(key, value, envir=cache)
assign(key, value, envir = identifyCache())
}

cacheKeys <- function () ls(all.names=TRUE, envir=cache)
cacheKeys <- function (){
setdiff(ls(all.names=TRUE, envir = identifyCache()), cache_identifier_key)
}

#' Construct a unique cache key for a request
#'
Expand Down Expand Up @@ -125,14 +127,14 @@ dropCache <- function (x) {
#' @export
dropOnly <- function (x) {
logMessage("CACHE DROP", x)
suppressWarnings(rm(list=x, envir=cache))
suppressWarnings(rm(list=x, envir=identifyCache()))
}

#' @rdname dropCache
#' @export
dropPattern <- function (x) {
logMessage("CACHE DROP", x)
rm(list=ls(envir=cache, pattern=x), envir=cache)
rm(list=ls(envir=cache, pattern=x), envir=identifyCache())
}

# dropBelow <- function (x) {
Expand Down
16 changes: 15 additions & 1 deletion R/load-cache.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@

## Create the cache env
cache <- NULL
initCache <- function () {
cache <<- new.env(hash=TRUE)
}
cache_identifier_key <- '__CACHE_IDENTIFIER_FUNCTION__'
#' @export
registerCache <- function(fn){
stopifnot(
'fn must be a function' = is.function(fn)
)
assign(cache_identifier_key, fn, envir=cache)
}
identifyCache <- function(){
idfun <- get(cache_identifier_key, envir = cache)
idfun()
}
initCache()
registerCache(function() cache)

#' Save and load cache state
#'
Expand All @@ -13,7 +27,7 @@ initCache()
#' @return Nothing; called for side effects.
#' @export
saveCache <- function (file) {
saveRDS(cache, file=file)
saveRDS(identifyCache(), file=file)
}

#' @rdname saveCache
Expand Down