Caching

cache_remember

Remember a value (get from cache or compute and store)

Attempts to retrieve a cached value by name. If the cache doesn't exist, is expired, or a refresh is requested, evaluates the expression and caches the result. This is the primary caching interface for expensive computations.


Usage

cache_remember(name, expr, file = NULL, expire_after = NULL, refresh = FALSE)

Arguments

Argument Description
name

The cache name (non-empty string identifier)

expr

The expression to evaluate and cache if cache miss occurs. Expression is evaluated in the parent frame.

file

Optional file path to store the cache (default: cache/{name}.rds)

expire_after

Optional expiration time in hours (default: from config)

refresh

Optional boolean or function that returns boolean to force refresh. If TRUE or if function returns TRUE, cache is invalidated and expression is re-evaluated.

Returns

The cached value (if cache hit) or the result of evaluating expr (if cache miss or refresh requested)

Examples

# Cache expensive computation
result <- cache_remember("my_analysis", {
  expensive_computation()
})

# Force refresh when data changes
result <- cache_remember("analysis", {
  run_analysis()
}, refresh = file.mtime("data.csv") > cache_time)

Source: R/cache_read.R