From 89bbffdf3c19fecd00391c2800fd7e90db77a482 Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Fri, 22 Nov 2024 15:57:02 +0000 Subject: [PATCH 1/3] add cachem to Imports in DESCRIPTION --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index b83ed4309..ced76728e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,6 +32,7 @@ BugReports: https://github.com/epiverse-trace/epiparameter/issues Depends: R (>= 4.1.0) Imports: + cachem, checkmate, cli, distcrete, From 6b4d8a3bc394f1fc81af34a5f3fe7af8f4ebc997 Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Fri, 22 Nov 2024 15:57:43 +0000 Subject: [PATCH 2/3] create empty env and cache DB in .onLoad --- R/zzz.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/R/zzz.R b/R/zzz.R index 4596c0f14..815a3b69b 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,3 +1,15 @@ +# package environment for storing global objects for caching +cache_env <- new.env(parent = emptyenv()) + .onLoad <- function(libname, pkgname) { options(epiparameter = epiparameter_options) # nolint undersirable_function_linter + + # create an in-memory cache + cache_env$cache <- cachem::cache_mem() + + # suppress DB loading message on package loading + db <- suppressMessages(epiparameter_db()) + + # store epiparameter library in cache + cache_env$cache$set("multi_epiparameter", db) } From 450afceab6636899843b5aa2d7e4937d1de0232b Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Fri, 22 Nov 2024 15:58:06 +0000 Subject: [PATCH 3/3] update epiparamter_db to use cached DB --- R/epiparameter_db.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/epiparameter_db.R b/R/epiparameter_db.R index aee5b4b89..7bfbfac72 100644 --- a/R/epiparameter_db.R +++ b/R/epiparameter_db.R @@ -129,7 +129,13 @@ epiparameter_db <- function(disease = "all", ) } - multi_epiparameter <- .read_epiparameter_db() + cache_db <- cache_env$cache$get("multi_epiparameter") + if (inherits(cache_db, "multi_epiparameter")) { + multi_epiparameter <- cache_env$cache$get("multi_epiparameter") + } else { + multi_epiparameter <- .read_epiparameter_db() + } + attrib <- attributes(multi_epiparameter) multi_epiparameter <- .filter_epiparameter_db(