Skip to content

Caching

rv cache’s aggressively to avoid re-compiling and re-downloading packages and repository databases that have already been used.

Within the cache, each source is its own subdirectory to ensure the correct version of the correct package is installed.

Before going into details and how to change your caching behavior, let’s first consider a quick example:

In this example, we’ll start with a config file, inspect the output of rv cache, and the file tree of the cache as well.

In this config file, we have two CRAN-like repositories and one git repository.

rproject.toml
[project]
name = "rv-cache"
r_version = "4.5"
repositories = [
{alias = "PPM", url = "https://packagemanager.posit.co/cran/2025-01-01"},
{alias = "PPM_new", url = "https://packagemanager.posit.co/cran/latest"},
]
dependencies = [
"fansi",
{ name = "R6", repository = "PPM_new", force_source = true },
{ name = "osinfo", git = "https://github.com/a2-ai/osinfo", branch = "main" },
]

The cache follows the XDG Base Directory Specification for Unix like systems and the Known Folder Locations on Windows. Within the cache root, rv contains its cache within an rv subdirectory.

The cache root can be tuned by setting the XDG_CACHE_HOME for Unix like systems and %LOCALAPPDATA% for Windows.

For additional tunability, use the GHQC_CACHE_DIR env var.

CRAN-type repositories use PACKAGES files that contain information about each package present in the repository for each package distribution (i.e. source, Mac R 4.5, ubuntu-24.04 R 4.4, etc.). rv uses the source PACKAGES file and distribution’s binary PACKAGES file to create a database for the repository.

This database is cached and times out according to the PKGCACHE_TIMEOUT environment variable (default 1 hour).

Within the rv cache, each source is its own subdirectory

The details of package caching vary based on the dependency type.

A repository cache is broken into two components: source and binaries. The binaries are also separated by their platform, a combination of R version, OS, and architecture. rv will fetch a package to be installed into your library in the following ways:

  1. Use the binary package found in the cache
  2. Download the binary package from the resolved source (ignored if force_source)
  3. Compile the package from the cached source package
  4. Download the source package from the resolved source and compile

rv keeps a source clone of the git repository under the git subdirectory, but then also maintains compiled binaries based on the git commits used.

rv keeps the downloaded package in cache, along with the compiled version if a source package. Upon re-installation of the package, the package is re-downloaded to ensure the contents match. If they do, the package currently in cache is retained, otherwise, the new version will take its place.

rv does not keep local packages in the cache. Since the file is already on the system, keeping a cached version is not practical. rv does however keep track of the hash of the source file within the compiled package in the library, therefore re-installation only occurs when the source file changes.

The global cache feature is available as of rv v0.18.0.

The global cache is a shared cache that organizations can configure to reduce the number of copies of packages across multiple machines and projects. Instead of each project and user maintaining their own complete copy of packages, projects can link against a centralized global cache, significantly reducing disk space usage and redundant downloads.

The global cache works in conjunction with your local cache using a multi-level caching strategy:

  1. Global Cache - A shared cache (RV_GLOBAL_CACHE_DIR) that is consulted first. If a package is present in the global cache, it is referenced directly from there
  2. Local Cache - Your project-specific cache (RV_CACHE_DIR) serves as a fallback for packages not found in the global cache

Linking - rv uses the same linking mechanism (hard-links, copy-on-write, or copying) to link packages from the global cache into your project, avoiding duplication

The global cache is generally configured by system administrators. It can be set using the RV_GLOBAL_CACHE_DIR environment variable to point to a shared location accessible to all users and machines.

For example, an administrator might configure:

Terminal window
export RV_GLOBAL_CACHE_DIR=/mnt/shared/rv-cache

This allows all users and projects on the organization to benefit from a shared pool of pre-compiled packages, reducing network bandwidth and storage costs while maintaining isolation through the local cache layer.