or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cache-management.mdfile-stores.mdhttp-stores.mdindex.mdutilities.md
tile.json

tessl/npm-metro-cache

Cache layers for Metro bundler with multi-layered caching system supporting local file storage and remote HTTP caching

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/metro-cache@0.83.x

To install, run

npx @tessl/cli install tessl/npm-metro-cache@0.83.0

index.mddocs/

Metro Cache

Metro 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.

Package Information

  • Package Name: metro-cache
  • Package Type: npm
  • Language: JavaScript (with Flow types)
  • Installation: npm install metro-cache

Core Imports

const {
  Cache,
  FileStore,
  AutoCleanFileStore,
  HttpStore,
  HttpGetStore,
  stableHash
} = require("metro-cache");

Basic Usage

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;

Architecture

Metro Cache is built around several key components:

  • Cache Orchestrator: The main Cache class coordinates access across multiple storage backends
  • Sequential Store Access: Cache stores are traversed in order, checking faster stores (FileStore) before slower ones (HttpStore)
  • Store Implementations: Various cache store implementations for different storage backends and use cases
  • Data Serialization: Automatic JSON serialization with special handling for binary Buffer data
  • Error Aggregation: Comprehensive error handling that aggregates failures across multiple stores

Capabilities

Cache Management

Core 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;
}

Cache Management

File-Based Storage

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;
}

File Storage

HTTP Remote Storage

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;
}

HTTP Storage

Utilities

Utility functions for cache key generation and type definitions.

function stableHash(value: mixed): Buffer;

Utilities

Types

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>;
}