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.00
# Metro Cache
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: metro-cache
7
- **Package Type**: npm
8
- **Language**: JavaScript (with Flow types)
9
- **Installation**: `npm install metro-cache`
10
11
## Core Imports
12
13
```javascript
14
const {
15
Cache,
16
FileStore,
17
AutoCleanFileStore,
18
HttpStore,
19
HttpGetStore,
20
stableHash
21
} = require("metro-cache");
22
```
23
24
## Basic Usage
25
26
```javascript
27
const { Cache, FileStore, HttpStore, stableHash } = require("metro-cache");
28
29
// Create cache stores
30
const fileStore = new FileStore({ root: "./cache" });
31
const httpStore = new HttpStore({
32
endpoint: "https://cache.example.com/api",
33
timeout: 5000
34
});
35
36
// Create multi-layered cache (checks fileStore first, then httpStore)
37
const cache = new Cache([fileStore, httpStore]);
38
39
// Generate cache key
40
const key = stableHash({ file: "index.js", transforms: ["minify"] });
41
42
// Use cache
43
const cachedResult = await cache.get(key);
44
if (cachedResult === null) {
45
const result = processFile(); // Your processing logic
46
await cache.set(key, result);
47
return result;
48
}
49
return cachedResult;
50
```
51
52
## Architecture
53
54
Metro Cache is built around several key components:
55
56
- **Cache Orchestrator**: The main `Cache` class coordinates access across multiple storage backends
57
- **Sequential Store Access**: Cache stores are traversed in order, checking faster stores (FileStore) before slower ones (HttpStore)
58
- **Store Implementations**: Various cache store implementations for different storage backends and use cases
59
- **Data Serialization**: Automatic JSON serialization with special handling for binary Buffer data
60
- **Error Aggregation**: Comprehensive error handling that aggregates failures across multiple stores
61
62
## Capabilities
63
64
### Cache Management
65
66
Core cache orchestration that manages sequential traversal through multiple cache stores with comprehensive logging and error handling.
67
68
```javascript { .api }
69
class Cache<T> {
70
constructor(stores: Array<CacheStore<T>>);
71
get(key: Buffer): Promise<T | null>;
72
set(key: Buffer, value: T): Promise<void>;
73
get isDisabled(): boolean;
74
}
75
```
76
77
[Cache Management](./cache-management.md)
78
79
### File-Based Storage
80
81
Local file system cache stores with support for both basic file storage and automatic cleanup of expired cache entries.
82
83
```javascript { .api }
84
class FileStore<T> {
85
constructor(options: FileOptions);
86
get(key: Buffer): Promise<T | null>;
87
set(key: Buffer, value: T): Promise<void>;
88
clear(): void;
89
}
90
91
interface FileOptions {
92
root: string;
93
}
94
```
95
96
[File Storage](./file-stores.md)
97
98
### HTTP Remote Storage
99
100
HTTP/HTTPS-based remote cache stores with compression, retry logic, and proxy support for distributed caching scenarios.
101
102
```javascript { .api }
103
class HttpStore<T> {
104
constructor(options: HttpOptions);
105
get(key: Buffer): Promise<T | null>;
106
set(key: Buffer, value: T): Promise<void>;
107
clear(): void;
108
}
109
110
interface HttpOptions {
111
endpoint: string;
112
timeout?: number;
113
headers?: { [string]: string };
114
maxAttempts?: number;
115
}
116
```
117
118
[HTTP Storage](./http-stores.md)
119
120
### Utilities
121
122
Utility functions for cache key generation and type definitions.
123
124
```javascript { .api }
125
function stableHash(value: mixed): Buffer;
126
```
127
128
[Utilities](./utilities.md)
129
130
## Types
131
132
```javascript { .api }
133
interface CacheStore<T> {
134
name?: string;
135
get(key: Buffer): T | null | Promise<T | null>;
136
set(key: Buffer, value: T): void | Promise<void>;
137
clear(): void | Promise<void>;
138
}
139
```