@kiln-js/cache 0.4.2 → 0.4.4

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.
Files changed (2) hide show
  1. package/dist/index.js +54 -59
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1,9 +1,8 @@
1
- // @bun
2
- // src/index.ts
3
- class MemoryCacheAdapter {
1
+ // packages/cache/src/index.ts
2
+ var MemoryCacheAdapter = class {
4
3
  name = "memory";
5
- store = new Map;
6
- tagIndex = new Map;
4
+ store = /* @__PURE__ */ new Map();
5
+ tagIndex = /* @__PURE__ */ new Map();
7
6
  maxEntries;
8
7
  defaultTtl;
9
8
  hitCount = 0;
@@ -43,8 +42,8 @@ class MemoryCacheAdapter {
43
42
  }
44
43
  const now = Date.now();
45
44
  const ttlSeconds = options.ttl ?? this.defaultTtl;
46
- const expiresAt = now + Math.max(0, ttlSeconds) * 1000;
47
- const staleUntil = options.staleWhileRevalidate && options.staleWhileRevalidate > 0 ? expiresAt + options.staleWhileRevalidate * 1000 : undefined;
45
+ const expiresAt = now + Math.max(0, ttlSeconds) * 1e3;
46
+ const staleUntil = options.staleWhileRevalidate && options.staleWhileRevalidate > 0 ? expiresAt + options.staleWhileRevalidate * 1e3 : void 0;
48
47
  const tags = Array.from(new Set(options.tags ?? []));
49
48
  const entry = {
50
49
  key,
@@ -61,7 +60,7 @@ class MemoryCacheAdapter {
61
60
  for (const tag of tags) {
62
61
  let set = this.tagIndex.get(tag);
63
62
  if (!set) {
64
- set = new Set;
63
+ set = /* @__PURE__ */ new Set();
65
64
  this.tagIndex.set(tag, set);
66
65
  }
67
66
  set.add(key);
@@ -70,15 +69,13 @@ class MemoryCacheAdapter {
70
69
  }
71
70
  async delete(key) {
72
71
  const existing = this.store.get(key);
73
- if (!existing)
74
- return false;
72
+ if (!existing) return false;
75
73
  this.store.delete(key);
76
74
  for (const tag of existing.tags) {
77
75
  const set = this.tagIndex.get(tag);
78
76
  if (set) {
79
77
  set.delete(key);
80
- if (set.size === 0)
81
- this.tagIndex.delete(tag);
78
+ if (set.size === 0) this.tagIndex.delete(tag);
82
79
  }
83
80
  }
84
81
  return true;
@@ -124,8 +121,7 @@ class MemoryCacheAdapter {
124
121
  const currentEntries = await this.entries();
125
122
  const tags = {};
126
123
  for (const [tag, keys] of this.tagIndex.entries()) {
127
- if (keys.size > 0)
128
- tags[tag] = Array.from(keys);
124
+ if (keys.size > 0) tags[tag] = Array.from(keys);
129
125
  }
130
126
  return {
131
127
  adapter: this.name,
@@ -137,13 +133,12 @@ class MemoryCacheAdapter {
137
133
  tags
138
134
  };
139
135
  }
140
- }
141
-
142
- class ExternalCacheAdapter {
136
+ };
137
+ var ExternalCacheAdapter = class {
143
138
  name;
144
139
  store;
145
- keyIndex = new Set;
146
- tagIndex = new Map;
140
+ keyIndex = /* @__PURE__ */ new Set();
141
+ tagIndex = /* @__PURE__ */ new Map();
147
142
  hitCount = 0;
148
143
  missCount = 0;
149
144
  invalidationCount = 0;
@@ -168,7 +163,7 @@ class ExternalCacheAdapter {
168
163
  }
169
164
  entry.hits = (entry.hits ?? 0) + 1;
170
165
  this.hitCount++;
171
- const remainingSeconds = Math.max(1, Math.ceil((hardLimit - now) / 1000));
166
+ const remainingSeconds = Math.max(1, Math.ceil((hardLimit - now) / 1e3));
172
167
  await this.store.set(key, JSON.stringify(entry), remainingSeconds);
173
168
  return entry;
174
169
  } catch {
@@ -180,8 +175,8 @@ class ExternalCacheAdapter {
180
175
  const now = Date.now();
181
176
  const ttlSeconds = options.ttl ?? 60;
182
177
  const swrSeconds = options.staleWhileRevalidate ?? 0;
183
- const expiresAt = now + Math.max(0, ttlSeconds) * 1000;
184
- const staleUntil = swrSeconds > 0 ? expiresAt + swrSeconds * 1000 : undefined;
178
+ const expiresAt = now + Math.max(0, ttlSeconds) * 1e3;
179
+ const staleUntil = swrSeconds > 0 ? expiresAt + swrSeconds * 1e3 : void 0;
185
180
  const tags = Array.from(new Set(options.tags ?? []));
186
181
  const entry = {
187
182
  key,
@@ -199,7 +194,7 @@ class ExternalCacheAdapter {
199
194
  for (const tag of tags) {
200
195
  let set = this.tagIndex.get(tag);
201
196
  if (!set) {
202
- set = new Set;
197
+ set = /* @__PURE__ */ new Set();
203
198
  this.tagIndex.set(tag, set);
204
199
  }
205
200
  set.add(key);
@@ -210,8 +205,7 @@ class ExternalCacheAdapter {
210
205
  this.keyIndex.delete(key);
211
206
  for (const [tag, set] of this.tagIndex.entries()) {
212
207
  set.delete(key);
213
- if (set.size === 0)
214
- this.tagIndex.delete(tag);
208
+ if (set.size === 0) this.tagIndex.delete(tag);
215
209
  }
216
210
  const res = await this.store.del(key);
217
211
  return Boolean(res);
@@ -251,14 +245,13 @@ class ExternalCacheAdapter {
251
245
  const now = Date.now();
252
246
  for (const key of keys) {
253
247
  const raw = await this.store.get(key);
254
- if (!raw)
255
- continue;
248
+ if (!raw) continue;
256
249
  try {
257
250
  const parsed = JSON.parse(raw);
258
251
  const limit = parsed.staleUntil ?? parsed.expiresAt;
259
- if (now <= limit)
260
- out.push(parsed);
261
- } catch {}
252
+ if (now <= limit) out.push(parsed);
253
+ } catch {
254
+ }
262
255
  }
263
256
  return out;
264
257
  }
@@ -266,8 +259,7 @@ class ExternalCacheAdapter {
266
259
  const active = await this.entries();
267
260
  const tags = {};
268
261
  for (const [tag, keys] of this.tagIndex.entries()) {
269
- if (keys.size > 0)
270
- tags[tag] = Array.from(keys);
262
+ if (keys.size > 0) tags[tag] = Array.from(keys);
271
263
  }
272
264
  return {
273
265
  adapter: this.name,
@@ -279,23 +271,21 @@ class ExternalCacheAdapter {
279
271
  tags
280
272
  };
281
273
  }
282
- }
283
-
284
- class TieredCacheAdapter {
285
- l1;
286
- l2;
287
- name = "tiered(memory+external)";
274
+ };
275
+ var TieredCacheAdapter = class {
288
276
  constructor(l1, l2) {
289
277
  this.l1 = l1;
290
278
  this.l2 = l2;
291
279
  }
280
+ l1;
281
+ l2;
282
+ name = "tiered(memory+external)";
292
283
  async get(key) {
293
284
  const fromL1 = await this.l1.get(key);
294
- if (fromL1)
295
- return fromL1;
285
+ if (fromL1) return fromL1;
296
286
  const fromL2 = await this.l2.get(key);
297
287
  if (fromL2) {
298
- const remainingSec = Math.max(1, Math.ceil((fromL2.expiresAt - Date.now()) / 1000));
288
+ const remainingSec = Math.max(1, Math.ceil((fromL2.expiresAt - Date.now()) / 1e3));
299
289
  await this.l1.set(key, fromL2.value, {
300
290
  ttl: remainingSec,
301
291
  tags: fromL2.tags,
@@ -317,11 +307,11 @@ class TieredCacheAdapter {
317
307
  }
318
308
  async invalidateTag(tag) {
319
309
  const [k1, k2] = await Promise.all([this.l1.invalidateTag(tag), this.l2.invalidateTag(tag)]);
320
- return Array.from(new Set([...k1, ...k2]));
310
+ return Array.from(/* @__PURE__ */ new Set([...k1, ...k2]));
321
311
  }
322
312
  async invalidatePath(pathPrefix) {
323
313
  const [k1, k2] = await Promise.all([this.l1.invalidatePath(pathPrefix), this.l2.invalidatePath(pathPrefix)]);
324
- return Array.from(new Set([...k1, ...k2]));
314
+ return Array.from(/* @__PURE__ */ new Set([...k1, ...k2]));
325
315
  }
326
316
  async entries() {
327
317
  return this.l2.entries();
@@ -339,14 +329,13 @@ class TieredCacheAdapter {
339
329
  tags: s2.tags
340
330
  };
341
331
  }
342
- }
343
-
344
- class DataCache {
332
+ };
333
+ var DataCache = class {
345
334
  adapter;
346
- inFlight = new Map;
335
+ inFlight = /* @__PURE__ */ new Map();
347
336
  dedupedHits = 0;
348
337
  constructor(adapter) {
349
- this.adapter = adapter ?? new MemoryCacheAdapter;
338
+ this.adapter = adapter ?? new MemoryCacheAdapter();
350
339
  }
351
340
  getAdapter() {
352
341
  return this.adapter;
@@ -354,6 +343,9 @@ class DataCache {
354
343
  setAdapter(adapter) {
355
344
  this.adapter = adapter;
356
345
  }
346
+ /**
347
+ * Deduplicates concurrent executions for the same key and caches the resolved value.
348
+ */
357
349
  async load(key, fetcher, options = {}) {
358
350
  const start = performance.now();
359
351
  if (!options.forceRefresh) {
@@ -362,7 +354,7 @@ class DataCache {
362
354
  const now = Date.now();
363
355
  const isStale = now > cached.expiresAt;
364
356
  if (isStale && cached.staleUntil && now <= cached.staleUntil) {
365
- this.revalidateInBackground(key, fetcher, options);
357
+ void this.revalidateInBackground(key, fetcher, options);
366
358
  return {
367
359
  data: cached.value,
368
360
  status: "STALE",
@@ -415,8 +407,7 @@ class DataCache {
415
407
  };
416
408
  }
417
409
  async revalidateInBackground(key, fetcher, options) {
418
- if (this.inFlight.has(key))
419
- return;
410
+ if (this.inFlight.has(key)) return;
420
411
  const p = (async () => {
421
412
  try {
422
413
  const fresh = await fetcher();
@@ -452,12 +443,12 @@ class DataCache {
452
443
  inFlightCount: this.inFlight.size
453
444
  };
454
445
  }
455
- }
446
+ };
456
447
  var GLOBAL_CACHE_KEY = "__KILN_DATA_CACHE__";
457
448
  function getGlobalDataCache() {
458
449
  const g = globalThis;
459
450
  if (!g[GLOBAL_CACHE_KEY]) {
460
- g[GLOBAL_CACHE_KEY] = new DataCache(new MemoryCacheAdapter);
451
+ g[GLOBAL_CACHE_KEY] = new DataCache(new MemoryCacheAdapter());
461
452
  }
462
453
  return g[GLOBAL_CACHE_KEY];
463
454
  }
@@ -480,12 +471,16 @@ function cachedLoader(loaderFn, config = {}, cacheInstance) {
480
471
  return async (ctx) => {
481
472
  const url = new URL(ctx.request.url);
482
473
  const computedKey = typeof config.key === "function" ? config.key(ctx) : config.key ?? `loader:${url.pathname}${url.search}`;
483
- const res = await cache.load(computedKey, async () => await loaderFn(ctx), {
484
- ttl: config.ttl ?? 60,
485
- staleWhileRevalidate: config.staleWhileRevalidate,
486
- tags: config.tags ?? [],
487
- path: config.path ?? url.pathname
488
- });
474
+ const res = await cache.load(
475
+ computedKey,
476
+ async () => await loaderFn(ctx),
477
+ {
478
+ ttl: config.ttl ?? 60,
479
+ staleWhileRevalidate: config.staleWhileRevalidate,
480
+ tags: config.tags ?? [],
481
+ path: config.path ?? url.pathname
482
+ }
483
+ );
489
484
  return res.data;
490
485
  };
491
486
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiln-js/cache",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -19,6 +19,6 @@
19
19
  "repository": "github:kiln-js/cache",
20
20
  "homepage": "https://kiln-js.vercel.app",
21
21
  "dependencies": {
22
- "@kiln-js/core": "^0.4.2"
22
+ "@kiln-js/core": "^0.4.4"
23
23
  }
24
24
  }