petty-cache 3.3.1 → 3.4.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/CHANGELOG.md +4 -0
- package/index.js +52 -22
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [3.4.0] - 2025-02-21
|
|
10
|
+
### Changed
|
|
11
|
+
- Added the ability for `pettyCache.mutex` functions to support callbacks and promises.
|
|
12
|
+
|
|
9
13
|
## [3.3.0] - 2024-07-03
|
|
10
14
|
### Changed
|
|
11
15
|
- Added the ability for `pettyCache.fetch` to support async functions.
|
package/index.js
CHANGED
|
@@ -461,42 +461,72 @@ function PettyCache() {
|
|
|
461
461
|
};
|
|
462
462
|
|
|
463
463
|
this.mutex = {
|
|
464
|
-
lock:
|
|
464
|
+
lock: (key, options, callback) => {
|
|
465
465
|
// Options are optional
|
|
466
466
|
if (!callback && typeof options === 'function') {
|
|
467
467
|
callback = options;
|
|
468
468
|
options = {};
|
|
469
469
|
}
|
|
470
470
|
|
|
471
|
-
callback = callback || function() {};
|
|
472
471
|
options = options || {};
|
|
473
472
|
|
|
474
|
-
options.retry = Object.
|
|
475
|
-
options.retry.interval = Object.
|
|
476
|
-
options.retry.times = Object.
|
|
477
|
-
options.ttl = Object.
|
|
473
|
+
options.retry = Object.hasOwn(options, 'retry') ? options.retry : {};
|
|
474
|
+
options.retry.interval = Object.hasOwn(options.retry, 'interval') ? options.retry.interval : 100;
|
|
475
|
+
options.retry.times = Object.hasOwn(options.retry, 'times') ? options.retry.times : 1;
|
|
476
|
+
options.ttl = Object.hasOwn(options, 'ttl') ? options.ttl : 1000;
|
|
478
477
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
478
|
+
const executor = () => {
|
|
479
|
+
return new Promise((resolve, reject) => {
|
|
480
|
+
async.retry({ interval: options.retry.interval, times: options.retry.times }, callback => {
|
|
481
|
+
redisClient.set(key, '1', 'NX', 'PX', options.ttl, function(err, res) {
|
|
482
|
+
if (err) {
|
|
483
|
+
return callback(err);
|
|
484
|
+
}
|
|
484
485
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
486
|
+
if (!res) {
|
|
487
|
+
return callback(new Error());
|
|
488
|
+
}
|
|
488
489
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
490
|
+
if (res !== 'OK') {
|
|
491
|
+
return callback(new Error(res));
|
|
492
|
+
}
|
|
492
493
|
|
|
493
|
-
|
|
494
|
+
callback();
|
|
495
|
+
});
|
|
496
|
+
}, function(err) {
|
|
497
|
+
if (err) {
|
|
498
|
+
return reject(err);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
resolve();
|
|
502
|
+
});
|
|
494
503
|
});
|
|
495
|
-
}
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
if (callback) {
|
|
507
|
+
executor().then(result => callback(null, result)).catch(callback);
|
|
508
|
+
} else {
|
|
509
|
+
return executor();
|
|
510
|
+
}
|
|
496
511
|
},
|
|
497
|
-
unlock:
|
|
498
|
-
|
|
499
|
-
|
|
512
|
+
unlock: (key, callback) => {
|
|
513
|
+
const executor = () => {
|
|
514
|
+
return new Promise((resolve, reject) => {
|
|
515
|
+
redisClient.del(key, function(err) {
|
|
516
|
+
if (err) {
|
|
517
|
+
return reject(err);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
resolve();
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
if (callback) {
|
|
526
|
+
executor().then(result => callback(null, result)).catch(callback);
|
|
527
|
+
} else {
|
|
528
|
+
return executor();
|
|
529
|
+
}
|
|
500
530
|
}
|
|
501
531
|
};
|
|
502
532
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"description": "A cache module for node.js that uses a two-level cache (in-memory cache for recently accessed data plus Redis for distributed caching) with some extra features to avoid cache stampedes and thundering herds.",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"async": "~3.2.
|
|
4
|
+
"async": "~3.2.6",
|
|
5
5
|
"lock": "~1.1.0",
|
|
6
6
|
"memory-cache": "~0.2.0",
|
|
7
7
|
"redis": "~3.1.0"
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"type": "git",
|
|
31
31
|
"url": "https://github.com/mediocre/petty-cache.git"
|
|
32
32
|
},
|
|
33
|
-
"version": "3.
|
|
33
|
+
"version": "3.4.0"
|
|
34
34
|
}
|