Caching Strategy & Storage
Relevant source files
The following files were used as context for generating this wiki page:
The regxa caching system is an optional decorator-based subsystem designed to add storage-backed reads, Time-To-Live (TTL) enforcement, and integrity checks to registry operations. It ensures that package metadata, version lists, dependencies, and maintainer information are stored locally to reduce network latency and prevent redundant API calls to external registries like npm, PyPI, and crates.io.
The architecture is built on top of the unstorage library, allowing for flexible storage drivers (filesystem by default, but extensible to Cloudflare KV or other backends). It utilizes a "lockfile" mechanism to track metadata about cached entries, including their fetch timestamps, integrity hashes (SHA-256), and TTL policies.
Sources: src/cache/AGENTS.md:3-8, README.md:129-137, src/cache/cached-registry.ts
Core Architecture
The caching system follows a read-through/write-through pattern. When a request is made through a CachedRegistry, the system first checks the lockfile for a fresh entry. If the data is present, fresh according to TTL, and passes an integrity check, it is returned from storage. Otherwise, it is fetched from the upstream registry, stored, and the lockfile is updated.
Components Relationship
The following diagram illustrates the relationship between the various caching modules:
The CachedRegistry acts as a wrapper around a standard Registry implementation, intercepting calls to methods like fetchPackage or fetchVersions.
Sources: src/cache/AGENTS.md:10-18, src/cache/cached-registry.ts:1-25
Lockfile and Freshness Policy
The system uses a lockfile to track the state of the cache. Every cached item has a corresponding LockfileEntry that defines when it was fetched and how long it remains valid.
TTL Configuration
Default TTL values are defined centrally to ensure consistent freshness policies across the application.
| Data Type | Default TTL (Seconds) | Description |
|---|---|---|
package | 3600 (1 hour) | General package metadata |
versions | 1800 (30 mins) | List of available versions |
dependencies | 86400 (24 hours) | Version-specific dependency tree |
maintainers | 86400 (24 hours) | Package maintainers/authors |
Sources: src/cache/lockfile.ts:6-12, README.md:189-195, test/unit/lockfile.test.ts:13-25
Entry Validation Logic
An entry is considered valid only if it meets two criteria:
- Freshness: The current time is less than
fetchedAt + (ttl * 1000). - Integrity: The data retrieved from storage must match the
integrityhash (SHA-256) stored in the lockfile.
Storage Implementation
Regxa leverages unstorage for its persistence layer. The storage keys are namespaced by ecosystem to prevent collisions between different registries (e.g., an npm package named json vs a cargo crate named json).
Directory Selection
Storage locations are platform-specific to adhere to OS conventions:
- Linux:
~/.cache/regxa(XDG) - macOS:
~/Library/Caches/regxa - Windows:
%LOCALAPPDATA%\regxa\cache
The path can be overridden using the REGXA_CACHE_DIR environment variable.
Sources: src/cache/paths.ts:5-15, README.md:139-144, src/cache/AGENTS.md:23
Storage Lifecycle
The storage system includes utility functions for maintenance, accessible via the CLI or the library API:
clearStorage(): Wipes all data and resets the lockfile.pruneStale(): Iterates through the lockfile and removes entries that have exceeded their TTL.computeIntegrity(): Generates the SHA-256 hash for data validation.
Sources: src/cache/storage.ts:35-50, src/commands/cache.ts:15-60, src/cache/lockfile.ts:90-105
Execution Flow: Cached Fetch
The following sequence diagram details the interaction between the user, the CachedRegistry, and the underlying storage during a standard fetch operation:
Single-Flight Deduplication
To optimize performance during concurrent operations, the CachedRegistry implements "single-flight" deduplication. If multiple calls are made for the same resource simultaneously while a fetch is already in progress, the system coalesces these requests. Subsequent callers wait for the first request to complete and share the result, preventing multiple redundant network requests.
Sources: CHANGELOG.md:14, test/unit/cached-registry.test.ts:340-365
Summary
The regxa caching strategy provides a robust, platform-aware storage layer that prioritizes data integrity and performance. By combining unstorage for data persistence with a metadata-rich lockfile for lifecycle management, it provides developers with a high-performance registry client that respects rate limits and minimizes network overhead.