@abtnode/db-cache 1.16.47-beta-20250715-034905-11207b15 → 1.16.47-beta-20250717-221700-2d886a18

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/index.cjs CHANGED
@@ -172,6 +172,7 @@ var __publicField$3 = (obj, key, value) => {
172
172
  __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
173
173
  return value;
174
174
  };
175
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
175
176
  async function initSqliteWithRetry(db) {
176
177
  const sql = `
177
178
  PRAGMA journal_mode = WAL;
@@ -205,6 +206,8 @@ const _SqliteAdapter = class _SqliteAdapter {
205
206
  constructor() {
206
207
  __publicField$3(this, "opts", null);
207
208
  __publicField$3(this, "prefix", "");
209
+ __publicField$3(this, "sqliteBusyRetryCount", 0);
210
+ __publicField$3(this, "sqliteBusyRetryGroupCount", 0);
208
211
  __publicField$3(this, "defaultTtl", 1e3 * 60 * 60);
209
212
  __publicField$3(this, "cleanupInterval", 5 * 60 * 1e3);
210
213
  __publicField$3(this, "dbPath", "");
@@ -312,38 +315,51 @@ const _SqliteAdapter = class _SqliteAdapter {
312
315
  if (exists)
313
316
  return false;
314
317
  }
315
- await new Promise((res, rej) => {
316
- client.run(
317
- `
318
+ try {
319
+ await new Promise((res, rej) => {
320
+ client.run(
321
+ `
318
322
  INSERT INTO kvcache (key, subKey, value, expiresAt)
319
323
  VALUES (?, ?, ?, ?)
320
324
  ON CONFLICT(key, subKey) DO UPDATE SET
321
325
  value = excluded.value,
322
326
  expiresAt = excluded.expiresAt
323
327
  `,
324
- [storageKey, subKey, this.serialize(value), expiresAt],
325
- (err) => err ? rej(err) : res()
326
- );
327
- });
328
+ [storageKey, subKey, this.serialize(value), expiresAt],
329
+ (err) => err ? rej(err) : res()
330
+ );
331
+ });
332
+ } catch (error) {
333
+ if (error.message?.includes("SQLITE_BUSY")) {
334
+ if (this.sqliteBusyRetryCount > 10) {
335
+ this.sqliteBusyRetryCount = 0;
336
+ throw error;
337
+ }
338
+ this.sqliteBusyRetryCount++;
339
+ await sleep(2e3);
340
+ const out = await this.set(key, value, opts);
341
+ this.sqliteBusyRetryCount = 0;
342
+ return out;
343
+ }
344
+ throw error;
345
+ }
328
346
  return true;
329
347
  }
330
348
  async get(key) {
331
349
  const client = this.getClient();
332
350
  const storageKey = this.prefixKey(key);
333
351
  const subKey = "";
334
- const row = await new Promise(
335
- (res, rej) => {
336
- client.get(
337
- 'SELECT value, "expiresAt" FROM kvcache WHERE key = ? AND subKey = ?',
338
- [storageKey, subKey],
339
- (err, result) => {
340
- if (err)
341
- return rej(err);
342
- return res(result);
343
- }
344
- );
345
- }
346
- );
352
+ const row = await new Promise((res, rej) => {
353
+ client.get(
354
+ 'SELECT value, "expiresAt" FROM kvcache WHERE key = ? AND subKey = ?',
355
+ [storageKey, subKey],
356
+ (err, result) => {
357
+ if (err)
358
+ return rej(err);
359
+ return res(result);
360
+ }
361
+ );
362
+ });
347
363
  if (!row)
348
364
  return null;
349
365
  if (row.expiresAt !== null && Date.now() > row.expiresAt) {
@@ -356,11 +372,7 @@ const _SqliteAdapter = class _SqliteAdapter {
356
372
  const client = this.getClient();
357
373
  const storageKey = this.prefixKey(key);
358
374
  await new Promise((res, rej) => {
359
- client.run(
360
- "DELETE FROM kvcache WHERE key = ?",
361
- [storageKey],
362
- (err) => err ? rej(err) : res()
363
- );
375
+ client.run("DELETE FROM kvcache WHERE key = ?", [storageKey], (err) => err ? rej(err) : res());
364
376
  });
365
377
  }
366
378
  async has(key) {
@@ -393,36 +405,49 @@ const _SqliteAdapter = class _SqliteAdapter {
393
405
  if (exists)
394
406
  return;
395
407
  }
396
- await new Promise((res, rej) => {
397
- client.run(
398
- `
408
+ try {
409
+ await new Promise((res, rej) => {
410
+ client.run(
411
+ `
399
412
  INSERT INTO kvcache (key, subKey, value, expiresAt)
400
413
  VALUES (?, ?, ?, ?)
401
414
  ON CONFLICT(key, subKey) DO UPDATE SET
402
415
  value = excluded.value,
403
416
  expiresAt = excluded.expiresAt
404
417
  `,
405
- [storageKey, subKey, this.serialize(value), expiresAt],
406
- (err) => err ? rej(err) : res()
407
- );
408
- });
418
+ [storageKey, subKey, this.serialize(value), expiresAt],
419
+ (err) => err ? rej(err) : res()
420
+ );
421
+ });
422
+ } catch (error) {
423
+ if (error.message?.includes("SQLITE_BUSY")) {
424
+ if (this.sqliteBusyRetryGroupCount > 10) {
425
+ this.sqliteBusyRetryGroupCount = 0;
426
+ throw error;
427
+ }
428
+ this.sqliteBusyRetryGroupCount++;
429
+ await sleep(2e3);
430
+ await this.groupSet(key, subKey, value, opts);
431
+ this.sqliteBusyRetryGroupCount = 0;
432
+ return;
433
+ }
434
+ throw error;
435
+ }
409
436
  }
410
437
  async groupGet(key, subKey) {
411
438
  const client = this.getClient();
412
439
  const storageKey = this.prefixKey(key);
413
- const row = await new Promise(
414
- (res, rej) => {
415
- client.get(
416
- 'SELECT value, "expiresAt" FROM kvcache WHERE key = ? AND subKey = ?',
417
- [storageKey, subKey],
418
- (err, result) => {
419
- if (err)
420
- return rej(err);
421
- return res(result);
422
- }
423
- );
424
- }
425
- );
440
+ const row = await new Promise((res, rej) => {
441
+ client.get(
442
+ 'SELECT value, "expiresAt" FROM kvcache WHERE key = ? AND subKey = ?',
443
+ [storageKey, subKey],
444
+ (err, result) => {
445
+ if (err)
446
+ return rej(err);
447
+ return res(result);
448
+ }
449
+ );
450
+ });
426
451
  if (!row)
427
452
  return null;
428
453
  if (row.expiresAt !== null && Date.now() > row.expiresAt) {
@@ -841,7 +866,7 @@ const getAbtNodeRedisAndSQLiteUrl = () => {
841
866
  const blockletCacheDir = process.env.BLOCKLET_DATA_DIR ? path__default.join(process.env.BLOCKLET_DATA_DIR, "__default-cache-store.db") : void 0;
842
867
  const params = {
843
868
  redisUrl: process.env.ABT_NODE_CACHE_REDIS_URL,
844
- sqlitePath: isJestTest() ? "test.db" : process.env.ABT_NODE_CACHE_SQLITE_PATH || blockletCacheDir
869
+ sqlitePath: isJestTest() ? "test.db" : blockletCacheDir || process.env.ABT_NODE_CACHE_SQLITE_PATH
845
870
  };
846
871
  if (process.env.ABT_NODE_NO_CACHE === "true") {
847
872
  params.ttl = 1;
package/dist/index.mjs CHANGED
@@ -152,6 +152,7 @@ var __publicField$3 = (obj, key, value) => {
152
152
  __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
153
153
  return value;
154
154
  };
155
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
155
156
  async function initSqliteWithRetry(db) {
156
157
  const sql = `
157
158
  PRAGMA journal_mode = WAL;
@@ -185,6 +186,8 @@ const _SqliteAdapter = class _SqliteAdapter {
185
186
  constructor() {
186
187
  __publicField$3(this, "opts", null);
187
188
  __publicField$3(this, "prefix", "");
189
+ __publicField$3(this, "sqliteBusyRetryCount", 0);
190
+ __publicField$3(this, "sqliteBusyRetryGroupCount", 0);
188
191
  __publicField$3(this, "defaultTtl", 1e3 * 60 * 60);
189
192
  __publicField$3(this, "cleanupInterval", 5 * 60 * 1e3);
190
193
  __publicField$3(this, "dbPath", "");
@@ -292,38 +295,51 @@ const _SqliteAdapter = class _SqliteAdapter {
292
295
  if (exists)
293
296
  return false;
294
297
  }
295
- await new Promise((res, rej) => {
296
- client.run(
297
- `
298
+ try {
299
+ await new Promise((res, rej) => {
300
+ client.run(
301
+ `
298
302
  INSERT INTO kvcache (key, subKey, value, expiresAt)
299
303
  VALUES (?, ?, ?, ?)
300
304
  ON CONFLICT(key, subKey) DO UPDATE SET
301
305
  value = excluded.value,
302
306
  expiresAt = excluded.expiresAt
303
307
  `,
304
- [storageKey, subKey, this.serialize(value), expiresAt],
305
- (err) => err ? rej(err) : res()
306
- );
307
- });
308
+ [storageKey, subKey, this.serialize(value), expiresAt],
309
+ (err) => err ? rej(err) : res()
310
+ );
311
+ });
312
+ } catch (error) {
313
+ if (error.message?.includes("SQLITE_BUSY")) {
314
+ if (this.sqliteBusyRetryCount > 10) {
315
+ this.sqliteBusyRetryCount = 0;
316
+ throw error;
317
+ }
318
+ this.sqliteBusyRetryCount++;
319
+ await sleep(2e3);
320
+ const out = await this.set(key, value, opts);
321
+ this.sqliteBusyRetryCount = 0;
322
+ return out;
323
+ }
324
+ throw error;
325
+ }
308
326
  return true;
309
327
  }
310
328
  async get(key) {
311
329
  const client = this.getClient();
312
330
  const storageKey = this.prefixKey(key);
313
331
  const subKey = "";
314
- const row = await new Promise(
315
- (res, rej) => {
316
- client.get(
317
- 'SELECT value, "expiresAt" FROM kvcache WHERE key = ? AND subKey = ?',
318
- [storageKey, subKey],
319
- (err, result) => {
320
- if (err)
321
- return rej(err);
322
- return res(result);
323
- }
324
- );
325
- }
326
- );
332
+ const row = await new Promise((res, rej) => {
333
+ client.get(
334
+ 'SELECT value, "expiresAt" FROM kvcache WHERE key = ? AND subKey = ?',
335
+ [storageKey, subKey],
336
+ (err, result) => {
337
+ if (err)
338
+ return rej(err);
339
+ return res(result);
340
+ }
341
+ );
342
+ });
327
343
  if (!row)
328
344
  return null;
329
345
  if (row.expiresAt !== null && Date.now() > row.expiresAt) {
@@ -336,11 +352,7 @@ const _SqliteAdapter = class _SqliteAdapter {
336
352
  const client = this.getClient();
337
353
  const storageKey = this.prefixKey(key);
338
354
  await new Promise((res, rej) => {
339
- client.run(
340
- "DELETE FROM kvcache WHERE key = ?",
341
- [storageKey],
342
- (err) => err ? rej(err) : res()
343
- );
355
+ client.run("DELETE FROM kvcache WHERE key = ?", [storageKey], (err) => err ? rej(err) : res());
344
356
  });
345
357
  }
346
358
  async has(key) {
@@ -373,36 +385,49 @@ const _SqliteAdapter = class _SqliteAdapter {
373
385
  if (exists)
374
386
  return;
375
387
  }
376
- await new Promise((res, rej) => {
377
- client.run(
378
- `
388
+ try {
389
+ await new Promise((res, rej) => {
390
+ client.run(
391
+ `
379
392
  INSERT INTO kvcache (key, subKey, value, expiresAt)
380
393
  VALUES (?, ?, ?, ?)
381
394
  ON CONFLICT(key, subKey) DO UPDATE SET
382
395
  value = excluded.value,
383
396
  expiresAt = excluded.expiresAt
384
397
  `,
385
- [storageKey, subKey, this.serialize(value), expiresAt],
386
- (err) => err ? rej(err) : res()
387
- );
388
- });
398
+ [storageKey, subKey, this.serialize(value), expiresAt],
399
+ (err) => err ? rej(err) : res()
400
+ );
401
+ });
402
+ } catch (error) {
403
+ if (error.message?.includes("SQLITE_BUSY")) {
404
+ if (this.sqliteBusyRetryGroupCount > 10) {
405
+ this.sqliteBusyRetryGroupCount = 0;
406
+ throw error;
407
+ }
408
+ this.sqliteBusyRetryGroupCount++;
409
+ await sleep(2e3);
410
+ await this.groupSet(key, subKey, value, opts);
411
+ this.sqliteBusyRetryGroupCount = 0;
412
+ return;
413
+ }
414
+ throw error;
415
+ }
389
416
  }
390
417
  async groupGet(key, subKey) {
391
418
  const client = this.getClient();
392
419
  const storageKey = this.prefixKey(key);
393
- const row = await new Promise(
394
- (res, rej) => {
395
- client.get(
396
- 'SELECT value, "expiresAt" FROM kvcache WHERE key = ? AND subKey = ?',
397
- [storageKey, subKey],
398
- (err, result) => {
399
- if (err)
400
- return rej(err);
401
- return res(result);
402
- }
403
- );
404
- }
405
- );
420
+ const row = await new Promise((res, rej) => {
421
+ client.get(
422
+ 'SELECT value, "expiresAt" FROM kvcache WHERE key = ? AND subKey = ?',
423
+ [storageKey, subKey],
424
+ (err, result) => {
425
+ if (err)
426
+ return rej(err);
427
+ return res(result);
428
+ }
429
+ );
430
+ });
406
431
  if (!row)
407
432
  return null;
408
433
  if (row.expiresAt !== null && Date.now() > row.expiresAt) {
@@ -821,7 +846,7 @@ const getAbtNodeRedisAndSQLiteUrl = () => {
821
846
  const blockletCacheDir = process.env.BLOCKLET_DATA_DIR ? path__default.join(process.env.BLOCKLET_DATA_DIR, "__default-cache-store.db") : void 0;
822
847
  const params = {
823
848
  redisUrl: process.env.ABT_NODE_CACHE_REDIS_URL,
824
- sqlitePath: isJestTest() ? "test.db" : process.env.ABT_NODE_CACHE_SQLITE_PATH || blockletCacheDir
849
+ sqlitePath: isJestTest() ? "test.db" : blockletCacheDir || process.env.ABT_NODE_CACHE_SQLITE_PATH
825
850
  };
826
851
  if (process.env.ABT_NODE_NO_CACHE === "true") {
827
852
  params.ttl = 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/db-cache",
3
- "version": "1.16.47-beta-20250715-034905-11207b15",
3
+ "version": "1.16.47-beta-20250717-221700-2d886a18",
4
4
  "description": "Db cache use redis or sqlite as backend",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -45,5 +45,5 @@
45
45
  "typescript": "^5.6.3",
46
46
  "unbuild": "^2.0.0"
47
47
  },
48
- "gitHead": "f08ee3ac63c808c93967db43445042b3050a015e"
48
+ "gitHead": "e8c5b883906dd53ba9c288cd5609cf7d023d90b3"
49
49
  }