Cache Integrity & Lockfile Management
Relevant source files
The following files were used as context for generating this wiki page:
The regxa project implements an optional decorator subsystem for package registry operations that provides storage-backed reads, Time-To-Live (TTL) enforcement, and SHA-256 integrity verification. This system ensures that metadata fetched from various registries (npm, PyPI, etc.) is stored efficiently and remains consistent across multiple sessions.
The architecture is built around a "read-through/write-through" flow where the cache layer wraps a core registry. It uses a lockfile to track metadata about cached entries, such as when they were fetched, their specific TTL policy, and a cryptographic hash of the content to prevent corruption.
Sources: src/cache/AGENTS.md, src/cache/cached-registry.ts:1-20, README.md
Core Architecture & Data Flow
The caching system is implemented as the CachedRegistry class, which decorates an inner Registry implementation. When a request is made, the system first checks the lockfile for a "fresh" entry. If a fresh entry exists and its integrity is verified, the data is returned from local storage. Otherwise, the system performs a network fetch, updates the storage, and records a new entry in the lockfile.
The diagram above illustrates the decision logic used by the CachedRegistry to decide between serving cached content or performing a network request.
Sources: src/cache/cached-registry.ts, src/cache/lockfile.ts, test/unit/cached-registry.test.ts
Lockfile Structure & Entry Management
The lockfile acts as the source of truth for cache metadata. It is a JSON structure containing a version number and a map of LockfileEntry objects.
Data Structures
| Type | Field | Description |
|---|---|---|
| Lockfile | version | Schema version (currently 1) |
entries | Record of keys to LockfileEntry | |
| LockfileEntry | key | Unique identifier (e.g., npm:lodash:package) |
type | Entry category: package, versions, dependencies, maintainers | |
fetchedAt | Timestamp in milliseconds of the last successful fetch | |
ttl | Duration in seconds the entry is considered fresh | |
integrity | SHA-256 hash of the stored data |
Sources: src/cache/lockfile.ts, test/unit/lockfile.test.ts:10-50
Cache Key Generation
Keys are generated using the cacheKey function, which namespaces entries by ecosystem, package name, data type, and optionally a version string. This preventing collisions between different registries or versions of the same package.
// Format: ecosystem:name:type[:version]
const key = cacheKey("npm", "lodash", "dependencies", "4.17.21");
// Result: "npm:lodash:dependencies:4.17.21"
Sources: src/cache/lockfile.ts, test/unit/lockfile.test.ts:40-65
Freshness & TTL Policies
The system applies different default TTL values based on the volatility of the data being cached. While dependency trees are relatively stable, version lists and general package metadata are updated more frequently.
| Entry Type | Default TTL | Duration |
|---|---|---|
package | 3600 | 1 Hour |
versions | 1800 | 30 Minutes |
dependencies | 86400 | 24 Hours |
maintainers | 86400 | 24 Hours |
The isFresh function determines validity by checking if Date.now() < fetchedAt + (ttl * 1000).
Sources: src/cache/lockfile.ts, test/unit/lockfile.test.ts:16-35
Integrity Verification
Integrity is managed through the computeIntegrity function in storage.ts. It generates a SHA-256 hash of the JSON-stringified value, prefixed with sha256-.
This sequence ensures that even if the underlying storage (filesystem or KV store) is modified externally, regxa will detect the corruption and refetch the data.
Sources: src/cache/storage.ts, src/cache/cached-registry.ts:135-150, test/unit/lockfile.test.ts:335-360
Performance Optimizations
The system includes several advanced features to handle high-concurrency environments:
- Single-flight Coalescing: Concurrent requests for the same cache key are deduped into a single upstream call. Subsequent callers wait for the first request to complete rather than triggering multiple network fetches.
- Atomic Lockfile Writes: To prevent race conditions, the system re-reads the lockfile immediately before writing to ensure no entries are lost during concurrent operations.
- Stale Pruning: The
pruneStaleutility iterates through the lockfile and removes entries that have exceeded their TTL, keeping the metadata footprint small.
Sources: src/cache/cached-registry.ts:40-60, CHANGELOG.md:19-21, src/commands/cache.ts:55-70
Storage Implementation
Regxa uses the unstorage library, allowing it to support multiple backends. By default, it uses a filesystem driver targeting OS-specific cache directories (e.g., ~/.cache/regxa on Linux).
Conclusion
Cache Integrity & Lockfile Management in regxa provides a robust mechanism for reducing network overhead while maintaining data accuracy. By combining strict TTL policies with cryptographic integrity checks and concurrency handling, the system ensures a reliable experience across diverse package ecosystems. Management of this system is exposed via CLI commands like regxa cache prune and regxa cache clear.
Sources: src/commands/cache.ts, README.md