File: old_cache.R

package info (click to toggle)
r-cran-memoise 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 208 kB
  • sloc: sh: 10; makefile: 5
file content (32 lines) | stat: -rw-r--r-- 699 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Wrap an old-style cache so that the external API is consistent with that from
# the cache package.

#' @importFrom cachem key_missing
wrap_old_cache <- function(x) {
  if (!is_old_cache(x)) {
    stop("`x` must be an old-style cache.", call. = FALSE)
  }

  list(
    digest = x$digest,
    reset = x$reset,
    set = x$set,
    get = function(key) {
      if (!x$has_key(key)) {
        return(key_missing())
      }
      x$get(key)
    },
    exists = x$has_key,
    remove = x$drop_key,
    keys = x$keys
  )
}

# Returns TRUE if it's an old-style cache.
is_old_cache <- function(x) {
  is.function(x$digest) &&
    is.function(x$set) &&
    is.function(x$get) &&
    is.function(x$has_key)
}