|
| 1 | +# Wraps a cachem cache so that NULL function results are never stored. |
| 2 | +# memoise stores results via withVisible(), so a NULL return is represented |
| 3 | +# as list(value = NULL, visible = ...). By skipping set() for those values |
| 4 | +# the cache misses on the next call, causing the underlying function to be |
| 5 | +# retried rather than returning the cached NULL error. |
| 6 | +null_filtering_cache <- function(cache) { |
| 7 | + list( |
| 8 | + get = function(key, ...) cache$get(key, ...), |
| 9 | + set = function(key, value) { |
| 10 | + if (is.list(value) && is.null(value$value)) { |
| 11 | + return(invisible(NULL)) |
| 12 | + } |
| 13 | + invisible(cache$set(key, value)) |
| 14 | + }, |
| 15 | + exists = function(key, ...) cache$exists(key, ...), |
| 16 | + remove = function(key, ...) cache$remove(key, ...), |
| 17 | + reset = function(...) cache$reset(...), |
| 18 | + keys = function(...) cache$keys(...), |
| 19 | + info = function(...) cache$info(...), |
| 20 | + prune = function(...) cache$prune(...) |
| 21 | + ) |
| 22 | +} |
| 23 | + |
1 | 24 | # nocov start |
2 | 25 | .onLoad <- function(libname, pkgname) { |
3 | 26 | reticulate::py_require("fastf1") |
|
28 | 51 | # set the cachedir to our new location for fastf1 caching too |
29 | 52 | options("f1dataR.cache" = cache_dir) |
30 | 53 | } |
31 | | - cache <- cachem::cache_disk(dir = memoise_option) |
| 54 | + cache <- null_filtering_cache(cachem::cache_disk(dir = memoise_option)) |
32 | 55 | } else if (memoise_option == "memory") { |
33 | | - cache <- cachem::cache_mem() |
| 56 | + cache <- null_filtering_cache(cachem::cache_mem()) |
34 | 57 | } |
35 | 58 |
|
36 | 59 | if (memoise_option != "off") { |
|
0 commit comments