-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
Hi Neal,
would the following addition make sense for the package? If so, would you be open to a PR or interested in implementing this yourself?
Problem
We have Shiny apps that use the crunch package, which in turns uses httpcache, as you might know. :)
Because of how Shiny Server works, different users share an R session, therefore they also share the httpcache's cache.
Instead of this, we'd like to have each user have their own cache.
Possible solution
We create a pair of functions that register/identify the cache environment:
cache_identifier_key <- '__CACHE_IDENTIFIER_FUNCTION__'
registerCacheIdentifier <- 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()
}We register the default environment in R/load-cache.R:
registerCacheIdentifier(function() cache)We then modify the functions that check/modify the cache environment:
hitCache <- function (key) {
exists(key, envir = identifyCache())
}
cacheKeys <- function (){
setdiff(ls(all.names=TRUE, envir = identifyCache()), cache_identifier_key)
}
# etc.Usage
Basic example
pkgload::load_all('.')
getCache('foo') # NULL
setCache('foo', 'bar')
getCache('foo') # bar
cache_env <- new.env()
registerCacheIdentifier(function() cache_env)
getCache('foo') # NULL
setCache('foo', 'bar!')
getCache('foo') # bar!Shiny
For our Shiny applications, we could then do something like this:
registerCacheIdentifier(function() shiny::getShinyOption('cache_environment'))Inside the server function:
shiny::shinyOptions(cache_environment = new.env(parent = emptyenv()))Metadata
Metadata
Assignees
Labels
No labels