opticore-cache 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,314 @@
1
+ # opticore-cache
2
+
3
+ ### Enterprise‑grade Universal HTTP Cache for OptiCoreJs and also Node.js
4
+
5
+ [![npm
6
+ version](https://img.shields.io/npm/v/@opticore/http-cache.svg)](https://www.npmjs.com/package/opticore-cache)
7
+ [![license](https://img.shields.io/npm/l/@opticore/http-cache.svg)](LICENSE)
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
9
+ [![Node](https://img.shields.io/badge/Node.js-18%2B-green.svg)](https://nodejs.org/)
10
+
11
+ High‑performance, flexible and production‑ready HTTP caching layer
12
+ compatible with\
13
+ **Express · Fetch · Axios · Native Node HTTP · cURL**
14
+ :::
15
+
16
+
17
+
18
+ # 📑 Table of Contents
19
+
20
+ - [Why opticore-cache ?](#why-opticore-cache)
21
+ - [Key Features](#key-features)
22
+ - [Installation](#installation)
23
+ - [Quick Start](#quick-start)
24
+ - [Configuration](#configuration)
25
+ - [Usage Examples](#usage-examples)
26
+ - [API Reference](#api-reference)
27
+ - [Cache Strategy & Architecture](#cache-strategy--architecture)
28
+ - [Performance Considerations](#performance-considerations)
29
+ - [Testing](#testing)
30
+ - [Security](#security)
31
+ - [License](#license)
32
+ - [Contributing](#contributing)
33
+
34
+
35
+
36
+ # Why opticore-cache?
37
+
38
+ Modern applications require:
39
+
40
+ - High availability
41
+ - Reduced external API load
42
+ - Faster response times
43
+ - Observability & metrics
44
+ - Flexible storage strategies
45
+
46
+ `@opticore/http-cache` provides a **unified abstraction layer** over
47
+ multiple HTTP clients with an extensible and configurable caching
48
+ engine.
49
+
50
+
51
+
52
+ # Key Features
53
+
54
+ - ✅ Automatic HTTP response caching
55
+ - ✅ Multi-client support (Fetch, Axios, Node HTTP, cURL, Express middleware)
56
+ - ✅ Memory, Disk or Hybrid storage
57
+ - ✅ Per-request TTL override
58
+ - ✅ Pattern-based invalidation (wildcards supported)
59
+ - ✅ Built-in statistics & hit-rate tracking
60
+ - ✅ Namespaces for multi-tenant systems
61
+ - ✅ TypeScript-first design
62
+ - ✅ Production-safe retry & timeout handling
63
+
64
+
65
+
66
+ # Installation
67
+
68
+ ``` bash
69
+ npm install opticore-cache
70
+ ```
71
+
72
+ ### Optional Dependencies
73
+
74
+ ``` bash
75
+ npm install axios express
76
+ ```
77
+
78
+
79
+ # Quick Start
80
+
81
+ ## Express Middleware (Zero‑Config)
82
+
83
+ ``` ts
84
+ import express from "express";
85
+ import { createExpressMiddleware } from "opticore-cache";
86
+
87
+ const app = express();
88
+
89
+ app.use(createExpressMiddleware({
90
+ cache: { ttl: 60000 }
91
+ }));
92
+
93
+ app.get("/users/:id", (req, res) => {
94
+ res.json({ id: req.params.id, name: "John Doe" });
95
+ });
96
+
97
+ app.listen(3000);
98
+ ```
99
+
100
+
101
+
102
+ # Configuration
103
+
104
+ ## Default Configuration
105
+
106
+ ``` ts
107
+ const defaultConfig = {
108
+ cache: {
109
+ enabled: true,
110
+ ttl: 300000,
111
+ maxSize: 10000,
112
+ storage: "disk", // memory | disk | hybrid
113
+ storageOptions: {
114
+ diskDir: "./storage/cache/http"
115
+ }
116
+ },
117
+ http: {
118
+ defaultClient: "fetch",
119
+ timeout: 30000,
120
+ retries: 3,
121
+ retryDelay: 1000
122
+ },
123
+ clients: {
124
+ fetch: { credentials: "omit" },
125
+ axios: { baseURL: "" },
126
+ nodeHttp: { keepAlive: true },
127
+ curl: { binaryPath: "/usr/bin/curl" }
128
+ }
129
+ };
130
+ ```
131
+
132
+
133
+
134
+ # Usage Examples
135
+
136
+ ## Fetch Client
137
+
138
+ ``` ts
139
+ import { HttpCacheFactory } from "@opticore/http-cache";
140
+
141
+ const cache = HttpCacheFactory.create("my-app", {
142
+ clientType: "fetch",
143
+ storageType: "disk",
144
+ diskDir: "./storage/cache/my-app",
145
+ defaultTTL: 60000
146
+ });
147
+
148
+ const user = await cache.getWithCache(
149
+ "https://jsonplaceholder.typicode.com/users/1",
150
+ {},
151
+ { timeToLive: 30000 }
152
+ );
153
+
154
+ console.log(user._metadata.cached ? "From cache" : "From API");
155
+ ```
156
+
157
+
158
+
159
+ ## Axios Client
160
+
161
+ ``` ts
162
+ const cache = HttpCacheFactory.create("axios-app", {
163
+ clientType: "axios",
164
+ clientOptions: {
165
+ baseURL: "https://jsonplaceholder.typicode.com",
166
+ timeout: 5000
167
+ }
168
+ });
169
+
170
+ const data = await cache.getWithCache("/users/1");
171
+ ```
172
+
173
+
174
+
175
+ # API Reference
176
+
177
+ ## Factory
178
+
179
+ ``` ts
180
+ HttpCacheFactory.create(appName: string, config?: HttpCacheConfig)
181
+ ```
182
+
183
+ Returns: `IHttpCacheService`
184
+
185
+
186
+
187
+ ## Core Methods
188
+
189
+ ### getWithCache`<T>`()
190
+
191
+ ``` ts
192
+ getWithCache<T>(
193
+ url: string,
194
+ options?: RequestInit,
195
+ cacheOptions?: {
196
+ timeToLive?: number;
197
+ customKey?: string;
198
+ bypassCache?: boolean;
199
+ namespace?: string;
200
+ }
201
+ ): Promise<T>
202
+ ```
203
+
204
+
205
+
206
+ ### postWithCache`<T>`()
207
+
208
+ Optional response caching for POST requests.
209
+
210
+
211
+
212
+ ### invalidateCache(pattern: string)
213
+
214
+ Wildcard invalidation:
215
+
216
+ ``` ts
217
+ await cache.invalidateCache("users:*");
218
+ ```
219
+
220
+
221
+
222
+ ### clearHttpCache()
223
+
224
+ Clears all entries.
225
+
226
+
227
+
228
+ ### getStats()
229
+
230
+ ``` ts
231
+ interface HttpCacheStats {
232
+ totalRequests: number;
233
+ cacheHits: number;
234
+ cacheMisses: number;
235
+ cacheSize: number;
236
+ hitRate: number;
237
+ cachedUrls: string[];
238
+ timestamp: string;
239
+ enabled: boolean;
240
+ }
241
+ ```
242
+
243
+
244
+
245
+ # Cache Strategy & Architecture
246
+
247
+ Supported storage strategies:
248
+
249
+ Strategy Use Case
250
+ ---------- ----------------------------------
251
+ memory High-speed, short-lived cache
252
+ disk Persistent cache across restarts
253
+ hybrid Memory-first with disk fallback
254
+
255
+ ### Hybrid Mode
256
+
257
+ - Memory lookup first
258
+ - Disk fallback
259
+ - Background refresh supported
260
+
261
+
262
+
263
+ # Performance Considerations
264
+
265
+ - Reduces outbound API calls
266
+ - Improves P95 & P99 latency
267
+ - Reduces infrastructure cost
268
+ - Prevents API rate-limit exhaustion
269
+
270
+ Recommended for:
271
+
272
+ - Microservices
273
+ - SaaS platforms
274
+ - Multi-tenant APIs
275
+ - Serverless environments
276
+
277
+
278
+
279
+ # Testing
280
+
281
+ ``` bash
282
+ npm test
283
+ ```
284
+
285
+
286
+
287
+ # Security
288
+
289
+ - No automatic caching of sensitive headers
290
+ - Namespace isolation supported
291
+ - Manual invalidation available
292
+ - Safe retry mechanism
293
+
294
+
295
+
296
+ # License
297
+
298
+ MIT
299
+
300
+
301
+
302
+ # Contributing
303
+
304
+ Contributions are welcome.
305
+
306
+ 1. Fork the repository
307
+ 2. Create a feature branch
308
+ 3. Submit a Pull Request
309
+
310
+
311
+
312
+ :::
313
+ Built for scalable OptiCoreJs and any Node.js applications
314
+ :::