layercache 2.0.0 → 2.1.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/dist/{chunk-7KMKQ6QZ.js → chunk-6X7NV5BG.js} +33 -0
- package/dist/{chunk-FFZCC7EQ.js → chunk-IVX6ABFX.js} +87 -0
- package/dist/cli.cjs +33 -0
- package/dist/cli.js +1 -1
- package/dist/{edge-D2FpRlyS.d.cts → edge-BCU8D-Yd.d.cts} +504 -0
- package/dist/{edge-D2FpRlyS.d.ts → edge-BCU8D-Yd.d.ts} +504 -0
- package/dist/edge.cjs +87 -0
- package/dist/edge.d.cts +1 -1
- package/dist/edge.d.ts +1 -1
- package/dist/edge.js +1 -1
- package/dist/index.cjs +485 -0
- package/dist/index.d.cts +244 -2
- package/dist/index.d.ts +244 -2
- package/dist/index.js +367 -2
- package/package.json +1 -1
package/dist/edge.cjs
CHANGED
|
@@ -108,6 +108,9 @@ var MemoryLayer = class {
|
|
|
108
108
|
onEvict;
|
|
109
109
|
entries = /* @__PURE__ */ new Map();
|
|
110
110
|
cleanupTimer;
|
|
111
|
+
/**
|
|
112
|
+
* Creates an in-memory cache layer.
|
|
113
|
+
*/
|
|
111
114
|
constructor(options = {}) {
|
|
112
115
|
this.name = options.name ?? "memory";
|
|
113
116
|
this.defaultTtl = options.ttl;
|
|
@@ -121,10 +124,16 @@ var MemoryLayer = class {
|
|
|
121
124
|
this.cleanupTimer.unref?.();
|
|
122
125
|
}
|
|
123
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Reads and unwraps a fresh value from memory.
|
|
129
|
+
*/
|
|
124
130
|
async get(key) {
|
|
125
131
|
const value = await this.getEntry(key);
|
|
126
132
|
return unwrapStoredValue(value);
|
|
127
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Reads the raw stored value or envelope from memory.
|
|
136
|
+
*/
|
|
128
137
|
async getEntry(key) {
|
|
129
138
|
const entry = this.entries.get(key);
|
|
130
139
|
if (!entry) {
|
|
@@ -143,12 +152,21 @@ var MemoryLayer = class {
|
|
|
143
152
|
}
|
|
144
153
|
return entry.value;
|
|
145
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Reads many raw entries from memory.
|
|
157
|
+
*/
|
|
146
158
|
async getMany(keys) {
|
|
147
159
|
return Promise.all(keys.map((key) => this.getEntry(key)));
|
|
148
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Writes many entries to memory.
|
|
163
|
+
*/
|
|
149
164
|
async setMany(entries) {
|
|
150
165
|
await Promise.all(entries.map((entry) => this.set(entry.key, entry.value, entry.ttl)));
|
|
151
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Stores a value in memory using the provided TTL or layer default TTL.
|
|
169
|
+
*/
|
|
152
170
|
async set(key, value, ttl = this.defaultTtl) {
|
|
153
171
|
this.entries.delete(key);
|
|
154
172
|
this.entries.set(key, {
|
|
@@ -161,6 +179,9 @@ var MemoryLayer = class {
|
|
|
161
179
|
this.evict();
|
|
162
180
|
}
|
|
163
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Returns true when the key exists and has not expired.
|
|
184
|
+
*/
|
|
164
185
|
async has(key) {
|
|
165
186
|
const entry = this.entries.get(key);
|
|
166
187
|
if (!entry) {
|
|
@@ -172,6 +193,9 @@ var MemoryLayer = class {
|
|
|
172
193
|
}
|
|
173
194
|
return true;
|
|
174
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Returns remaining TTL in milliseconds, or null when absent or non-expiring.
|
|
198
|
+
*/
|
|
175
199
|
async ttl(key) {
|
|
176
200
|
const entry = this.entries.get(key);
|
|
177
201
|
if (!entry) {
|
|
@@ -186,40 +210,67 @@ var MemoryLayer = class {
|
|
|
186
210
|
}
|
|
187
211
|
return Math.max(0, Math.ceil(entry.expiresAt - Date.now()));
|
|
188
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Returns the number of currently retained, non-expired entries.
|
|
215
|
+
*/
|
|
189
216
|
async size() {
|
|
190
217
|
this.pruneExpired();
|
|
191
218
|
return this.entries.size;
|
|
192
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Deletes a key from memory.
|
|
222
|
+
*/
|
|
193
223
|
async delete(key) {
|
|
194
224
|
this.entries.delete(key);
|
|
195
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Deletes multiple keys from memory.
|
|
228
|
+
*/
|
|
196
229
|
async deleteMany(keys) {
|
|
197
230
|
for (const key of keys) {
|
|
198
231
|
this.entries.delete(key);
|
|
199
232
|
}
|
|
200
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Removes all entries from memory.
|
|
236
|
+
*/
|
|
201
237
|
async clear() {
|
|
202
238
|
this.entries.clear();
|
|
203
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* Health check hook that always succeeds for the in-process layer.
|
|
242
|
+
*/
|
|
204
243
|
async ping() {
|
|
205
244
|
return true;
|
|
206
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* Stops the cleanup timer, when one is active.
|
|
248
|
+
*/
|
|
207
249
|
async dispose() {
|
|
208
250
|
if (this.cleanupTimer) {
|
|
209
251
|
clearInterval(this.cleanupTimer);
|
|
210
252
|
this.cleanupTimer = void 0;
|
|
211
253
|
}
|
|
212
254
|
}
|
|
255
|
+
/**
|
|
256
|
+
* Returns all currently retained, non-expired keys.
|
|
257
|
+
*/
|
|
213
258
|
async keys() {
|
|
214
259
|
this.pruneExpired();
|
|
215
260
|
return [...this.entries.keys()];
|
|
216
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* Visits all currently retained, non-expired keys.
|
|
264
|
+
*/
|
|
217
265
|
async forEachKey(visitor) {
|
|
218
266
|
this.pruneExpired();
|
|
219
267
|
for (const key of this.entries.keys()) {
|
|
220
268
|
await visitor(key);
|
|
221
269
|
}
|
|
222
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* Exports memory entries for process-local snapshots.
|
|
273
|
+
*/
|
|
223
274
|
exportState() {
|
|
224
275
|
this.pruneExpired();
|
|
225
276
|
return [...this.entries.entries()].map(([key, entry]) => ({
|
|
@@ -228,6 +279,9 @@ var MemoryLayer = class {
|
|
|
228
279
|
expiresAt: entry.expiresAt
|
|
229
280
|
}));
|
|
230
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Imports entries previously produced by `exportState()`.
|
|
284
|
+
*/
|
|
231
285
|
importState(entries) {
|
|
232
286
|
for (const entry of entries) {
|
|
233
287
|
if (entry.expiresAt !== null && entry.expiresAt <= Date.now()) {
|
|
@@ -346,10 +400,16 @@ var TagIndex = class {
|
|
|
346
400
|
constructor(options = {}) {
|
|
347
401
|
this.maxKnownKeys = options.maxKnownKeys ?? 1e5;
|
|
348
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* Records a key as known without changing tag assignments.
|
|
405
|
+
*/
|
|
349
406
|
async touch(key) {
|
|
350
407
|
this.insertKnownKey(key);
|
|
351
408
|
this.pruneKnownKeysIfNeeded();
|
|
352
409
|
}
|
|
410
|
+
/**
|
|
411
|
+
* Replaces the tags associated with a key and records the key as known.
|
|
412
|
+
*/
|
|
353
413
|
async track(key, tags) {
|
|
354
414
|
this.insertKnownKey(key);
|
|
355
415
|
this.pruneKnownKeysIfNeeded();
|
|
@@ -370,17 +430,29 @@ var TagIndex = class {
|
|
|
370
430
|
this.tagToKeys.set(tag, keys);
|
|
371
431
|
}
|
|
372
432
|
}
|
|
433
|
+
/**
|
|
434
|
+
* Removes a key from all tag mappings and known-key tracking.
|
|
435
|
+
*/
|
|
373
436
|
async remove(key) {
|
|
374
437
|
this.removeKey(key);
|
|
375
438
|
}
|
|
439
|
+
/**
|
|
440
|
+
* Returns keys currently associated with a tag.
|
|
441
|
+
*/
|
|
376
442
|
async keysForTag(tag) {
|
|
377
443
|
return [...this.tagToKeys.get(tag) ?? /* @__PURE__ */ new Set()];
|
|
378
444
|
}
|
|
445
|
+
/**
|
|
446
|
+
* Visits keys currently associated with a tag.
|
|
447
|
+
*/
|
|
379
448
|
async forEachKeyForTag(tag, visitor) {
|
|
380
449
|
for (const key of this.tagToKeys.get(tag) ?? /* @__PURE__ */ new Set()) {
|
|
381
450
|
await visitor(key);
|
|
382
451
|
}
|
|
383
452
|
}
|
|
453
|
+
/**
|
|
454
|
+
* Returns known keys that start with a prefix.
|
|
455
|
+
*/
|
|
384
456
|
async keysForPrefix(prefix) {
|
|
385
457
|
const node = this.findNode(prefix);
|
|
386
458
|
if (!node) {
|
|
@@ -390,6 +462,9 @@ var TagIndex = class {
|
|
|
390
462
|
this.collectFromNode(node, prefix, matches);
|
|
391
463
|
return matches;
|
|
392
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* Visits known keys that start with a prefix.
|
|
467
|
+
*/
|
|
393
468
|
async forEachKeyForPrefix(prefix, visitor) {
|
|
394
469
|
const node = this.findNode(prefix);
|
|
395
470
|
if (!node) {
|
|
@@ -397,20 +472,32 @@ var TagIndex = class {
|
|
|
397
472
|
}
|
|
398
473
|
await this.visitFromNode(node, prefix, visitor);
|
|
399
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* Returns the tags currently associated with a key.
|
|
477
|
+
*/
|
|
400
478
|
async tagsForKey(key) {
|
|
401
479
|
return [...this.keyToTags.get(key) ?? /* @__PURE__ */ new Set()];
|
|
402
480
|
}
|
|
481
|
+
/**
|
|
482
|
+
* Returns known keys matching a wildcard pattern.
|
|
483
|
+
*/
|
|
403
484
|
async matchPattern(pattern) {
|
|
404
485
|
const matches = /* @__PURE__ */ new Set();
|
|
405
486
|
this.collectPatternMatches(this.root, "", pattern, 0, matches, /* @__PURE__ */ new Set(), 0);
|
|
406
487
|
return [...matches];
|
|
407
488
|
}
|
|
489
|
+
/**
|
|
490
|
+
* Visits known keys matching a wildcard pattern.
|
|
491
|
+
*/
|
|
408
492
|
async forEachKeyMatchingPattern(pattern, visitor) {
|
|
409
493
|
const matches = await this.matchPattern(pattern);
|
|
410
494
|
for (const key of matches) {
|
|
411
495
|
await visitor(key);
|
|
412
496
|
}
|
|
413
497
|
}
|
|
498
|
+
/**
|
|
499
|
+
* Clears all tag and known-key index state.
|
|
500
|
+
*/
|
|
414
501
|
async clear() {
|
|
415
502
|
this.tagToKeys.clear();
|
|
416
503
|
this.keyToTags.clear();
|
package/dist/edge.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-
|
|
1
|
+
export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BCU8D-Yd.cjs';
|
|
2
2
|
import 'node:events';
|
package/dist/edge.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-
|
|
1
|
+
export { m as CacheContextOptionsContext, o as CacheEntryWriteKind, p as CacheEntryWriteOptions, e as CacheGetOptions, f as CacheLayer, h as CacheLayerSetManyEntry, y as CacheMetricsSnapshot, B as CacheRateLimitOptions, H as CacheTtlPolicy, J as CacheTtlPolicyContext, O as CacheWriteOptions, P as EvictionPolicy, R as MemoryLayer, S as MemoryLayerOptions, T as MemoryLayerSnapshotEntry, U as PatternMatcher, V as TagIndex, W as createHonoCacheMiddleware } from './edge-BCU8D-Yd.js';
|
|
2
2
|
import 'node:events';
|