We will build a small cache of market prices: store quotes under keys, find them again, let them expire, drop a group of them at once, and memoise the slow function that fetches them. By the end you will know the handful of functions most code needs. Running a cache across many processes, and how it is stored, are left to the articles listed at the end.
A cache is a directory. cache() opens
it, creating it if needed, and every R process on the machine that opens
the same directory sees the same entries. Its handle, s
here, is what every other function takes first.
cache_set() stores a value under a key
and returns the cache, so writes chain. cache_get() reads a
value back.
s |>
cache_set("XSWX/2026-08-28", c(100.8, 101.4)) |>
cache_set("XSWX/2026-08-29", c(101.2, 101.9))
cache_get(s, "XSWX/2026-08-29")
#> [1] 101.2 101.9Asking for a key that has no entry is the mistake you will make first. It is an error, so a missing value never passes for a real one:
cache_get(s, "XNYS/2026-08-29")
#> Error in `cache_get()`:
#> ! No entry for key "XNYS/2026-08-29".
#> ℹ Supply `default` to return a value instead of erroring.Supply default when a miss is expected, or ask with
cache_has() first. A stored NULL is a value
like any other, so the two never get confused.
A string is its own key, and a "group/item" convention
gives you prefixes to list by:
A key can also be a number, a date or a named list. The same values always find the same entry, whatever order the fields are written in:
An entry can expire: after a number of seconds, a
difftime, or at a POSIXct time. An expired
entry is gone for every reader at once. Here, an intraday quote is good
for an hour, and one with expire = 0 is gone as soon as it
is written:
cache_set(s, "XSWX/live", 101.6, expire = as.difftime(1, units = "hours"))
cache_set(s, "XSWX/stale", 101.5, expire = 0)
cache_has(s, c("XSWX/live", "XSWX/stale"))
#> [1] TRUE FALSETags group entries for invalidation. Tag the quotes that came from one feed, and drop them all when that feed turns out to be wrong:
Most quotes come from somewhere slow. cache_memoise()
wraps a function so that each call is looked up first and computed only
on a miss. The results outlive the session, and every process using the
cache shares them. The second call below reads the first call’s
result:
fetch_quotes <- function(exchange, date) {
Sys.sleep(0.5)
set.seed(as.integer(date))
round(runif(2, 50, 150), 1)
}
quotes <- cache_memoise(fetch_quotes, s)
system.time(quotes("XTKS", as.Date("2026-08-29")))
#> user system elapsed
#> 0.003 0.000 0.503
system.time(quotes("XTKS", as.Date("2026-08-29")))
#> user system elapsed
#> 0.001 0.000 0.001A call is keyed on the function’s name, here
fetch_quotes, the name it was passed as, and on its
arguments. The key does not depend on the function’s code: when the
function changes meaning, give it a new version, and its
calls get new keys.
A year of ticks is too large to keep inside the database. A value of
32 KiB or more (inline_max) is stored as a read-only RDS
file, and cache_path() gives that file to tools that read
it in place:
ticks <- runif(1e5, 100, 102)
cache_set(s, "XSWX/2026/ticks", ticks)
path <- cache_path(s, "XSWX/2026/ticks")
identical(readRDS(path), ticks)
#> [1] TRUEcache_entries() lists what the cache holds as a data
frame: each key, its size in bytes as stored, whether it lives inside
the database, and when it expires. A key that is not a string is listed
by its text form, which How a
cache stores keys and values explains.
cache_entries(s)[, c("key", "bytes", "inline", "expires")]
#> key bytes inline
#> 1 XSWX/2026-08-28 47 TRUE
#> 2 XSWX/2026-08-29 47 TRUE
#> 3 XSWX/2026/ticks 800031 FALSE
#> 4 XSWX/live 39 TRUE
#> 5 fetch_quotes/v1/{date=d:2026-08-29,exchange=s:XTKS} 47 TRUE
#> 6 {date=d:2026-08-29,exchange=s:XNYS} 47 TRUE
#> expires
#> 1 <NA>
#> 2 <NA>
#> 3 <NA>
#> 4 2026-12-01 10:56:27
#> 5 <NA>
#> 6 <NA>When you are done, close the cache. Its entries stay on disk for the next process.