@hkdigital/lib-sveltekit 0.1.96 → 0.1.97

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.
@@ -38,16 +38,19 @@
38
38
 
39
39
  /** @typedef {import('./typedef').IDBVersionChangeEvent} IDBVersionChangeEvent */
40
40
 
41
- const DEFAULT_DB_NAME ='http-cache';
42
- const DEFAULT_STORE_NAME ='responses';
43
- const DEFAULT_MAX_SIZE = 50 * 1024 * 1024; // 50 MB
44
- const DEFAULT_MAX_AGE = 90 * 24 * 60 * 60 * 1000; // 90 days
41
+ const DEFAULT_DB_NAME = 'http-cache';
42
+ const DEFAULT_STORE_NAME = 'responses';
43
+ const DEFAULT_MAX_SIZE = 50 * 1024 * 1024; // 50 MB
44
+ const DEFAULT_MAX_AGE = 90 * 24 * 60 * 60 * 1000; // 90 days
45
45
 
46
46
  const DEFAULT_CLEANUP_BATCH_SIZE = 100;
47
47
  const DEFAULT_CLEANUP_INTERVAL = 5 * 60 * 1000; // 5 minutes;
48
48
 
49
49
  const DEFAULT_CLEANUP_POSTPONE_MS = 5000; // 5 seconds
50
50
 
51
+ // Add logging to track concurrent access
52
+ const concurrentReadsByKey = new Map();
53
+
51
54
  /**
52
55
  * IndexedDbCache with automatic background cleanup
53
56
  */
@@ -72,10 +75,12 @@ export default class IndexedDbCache {
72
75
  this.maxSize = options.maxSize || DEFAULT_MAX_SIZE;
73
76
  this.maxAge = options.maxAge || DEFAULT_MAX_AGE;
74
77
 
75
- this.cleanupBatchSize = options.cleanupBatchSize || DEFAULT_CLEANUP_BATCH_SIZE;
78
+ this.cleanupBatchSize =
79
+ options.cleanupBatchSize || DEFAULT_CLEANUP_BATCH_SIZE;
76
80
  this.cleanupInterval = options.cleanupInterval || DEFAULT_CLEANUP_INTERVAL;
77
81
 
78
- this.cleanupPostponeTimeout = options.cleanupPostponeTimeout || DEFAULT_CLEANUP_POSTPONE_MS;
82
+ this.cleanupPostponeTimeout =
83
+ options.cleanupPostponeTimeout || DEFAULT_CLEANUP_POSTPONE_MS;
79
84
  this.cacheVersion = options.cacheVersion || '1.0.0';
80
85
 
81
86
  // Define index names as constants to ensure consistency
@@ -318,10 +323,15 @@ export default class IndexedDbCache {
318
323
  * @returns {Promise<CacheEntry|null>} Cache entry or null if not found/expired
319
324
  */
320
325
  async get(key) {
326
+ // Track concurrent reads per key
327
+ const current = concurrentReadsByKey.get(key) || 0;
328
+ concurrentReadsByKey.set(key, current + 1);
329
+ console.log(`Concurrent reads for ${key}: ${current + 1}`);
330
+
321
331
  try {
322
332
  const db = await this.dbPromise;
323
333
 
324
- return new Promise((resolve, reject) => {
334
+ const result = new Promise((resolve, reject) => {
325
335
  try {
326
336
  const transaction = db.transaction(this.storeName, 'readonly');
327
337
  const store = transaction.objectStore(this.storeName);
@@ -421,9 +431,22 @@ export default class IndexedDbCache {
421
431
  resolve(null);
422
432
  }
423
433
  });
434
+
435
+ return result;
424
436
  } catch (err) {
425
437
  console.error('Cache get error:', err);
426
438
  return null;
439
+ } finally {
440
+ // Always decrement
441
+ const current = concurrentReadsByKey.get(key) || 1;
442
+ if (current <= 1) {
443
+ concurrentReadsByKey.delete(key);
444
+ } else {
445
+ concurrentReadsByKey.set(key, current - 1);
446
+ }
447
+ console.log(
448
+ `Concurrent reads for ${key} after decrement: ${current - 1}`
449
+ );
427
450
  }
428
451
  }
429
452
 
@@ -490,7 +513,7 @@ export default class IndexedDbCache {
490
513
  // Calculate rough size estimate
491
514
  const headerSize = JSON.stringify(headers).length * 2;
492
515
  const size =
493
- /** @type {Blob} */ ((body).size || 0) + headerSize + key.length * 2;
516
+ /** @type {Blob} */ (body.size || 0) + headerSize + key.length * 2;
494
517
 
495
518
  const entry = {
496
519
  key,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.1.96",
3
+ "version": "0.1.97",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"