Cache layers for Metro bundler with multi-layered caching system supporting local file storage and remote HTTP caching
npx @tessl/cli install tessl/npm-metro-cache@0.83.0Metro Cache provides comprehensive caching functionality for Metro bundler with a multi-layered cache system that supports various storage backends including local file storage and remote HTTP caching. It implements sequential traversal through different cache stores to optimize build performance for React Native development.
npm install metro-cacheconst {
Cache,
FileStore,
AutoCleanFileStore,
HttpStore,
HttpGetStore,
stableHash
} = require("metro-cache");const { Cache, FileStore, HttpStore, stableHash } = require("metro-cache");
// Create cache stores
const fileStore = new FileStore({ root: "./cache" });
const httpStore = new HttpStore({
endpoint: "https://cache.example.com/api",
timeout: 5000
});
// Create multi-layered cache (checks fileStore first, then httpStore)
const cache = new Cache([fileStore, httpStore]);
// Generate cache key
const key = stableHash({ file: "index.js", transforms: ["minify"] });
// Use cache
const cachedResult = await cache.get(key);
if (cachedResult === null) {
const result = processFile(); // Your processing logic
await cache.set(key, result);
return result;
}
return cachedResult;Metro Cache is built around several key components:
Cache class coordinates access across multiple storage backendsCore cache orchestration that manages sequential traversal through multiple cache stores with comprehensive logging and error handling.
class Cache<T> {
constructor(stores: Array<CacheStore<T>>);
get(key: Buffer): Promise<T | null>;
set(key: Buffer, value: T): Promise<void>;
get isDisabled(): boolean;
}Local file system cache stores with support for both basic file storage and automatic cleanup of expired cache entries.
class FileStore<T> {
constructor(options: FileOptions);
get(key: Buffer): Promise<T | null>;
set(key: Buffer, value: T): Promise<void>;
clear(): void;
}
interface FileOptions {
root: string;
}HTTP/HTTPS-based remote cache stores with compression, retry logic, and proxy support for distributed caching scenarios.
class HttpStore<T> {
constructor(options: HttpOptions);
get(key: Buffer): Promise<T | null>;
set(key: Buffer, value: T): Promise<void>;
clear(): void;
}
interface HttpOptions {
endpoint: string;
timeout?: number;
headers?: { [string]: string };
maxAttempts?: number;
}Utility functions for cache key generation and type definitions.
function stableHash(value: mixed): Buffer;interface CacheStore<T> {
name?: string;
get(key: Buffer): T | null | Promise<T | null>;
set(key: Buffer, value: T): void | Promise<void>;
clear(): void | Promise<void>;
}