petty-cache 3.3.0 → 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.
Files changed (3) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/index.js +55 -25
  3. 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
@@ -1,5 +1,3 @@
1
- const util = require('util');
2
-
3
1
  const async = require('async');
4
2
  const lock = require('lock').Lock();
5
3
  const memoryCache = require('memory-cache');
@@ -336,7 +334,8 @@ function PettyCache() {
336
334
  }
337
335
 
338
336
  // Execute the specified function and place the results in cache before returning the data
339
- if (util.types.isAsyncFunction(func)) {
337
+ if (func.length === 0) {
338
+ // If the function doesn't have any arguments, there wasn't a callback provided
340
339
  try {
341
340
  const data = await func();
342
341
 
@@ -347,6 +346,7 @@ function PettyCache() {
347
346
  callback(err);
348
347
  }
349
348
  } else {
349
+ // If the function has arguments, there was a callback provided
350
350
  func(function(err, data) {
351
351
  if (err) {
352
352
  return callback(err);
@@ -461,42 +461,72 @@ function PettyCache() {
461
461
  };
462
462
 
463
463
  this.mutex = {
464
- lock: function(key, options, callback) {
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.prototype.hasOwnProperty.call(options, 'retry') ? options.retry : {};
475
- options.retry.interval = Object.prototype.hasOwnProperty.call(options.retry, 'interval') ? options.retry.interval : 100;
476
- options.retry.times = Object.prototype.hasOwnProperty.call(options.retry, 'times') ? options.retry.times : 1;
477
- options.ttl = Object.prototype.hasOwnProperty.call(options, 'ttl') ? options.ttl : 1000;
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
- async.retry({ interval: options.retry.interval, times: options.retry.times }, function(callback) {
480
- redisClient.set(key, '1', 'NX', 'PX', options.ttl, function(err, res) {
481
- if (err) {
482
- return callback(err);
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
- if (!res) {
486
- return callback(new Error());
487
- }
486
+ if (!res) {
487
+ return callback(new Error());
488
+ }
488
489
 
489
- if (res !== 'OK') {
490
- return callback(new Error(res));
491
- }
490
+ if (res !== 'OK') {
491
+ return callback(new Error(res));
492
+ }
493
+
494
+ callback();
495
+ });
496
+ }, function(err) {
497
+ if (err) {
498
+ return reject(err);
499
+ }
492
500
 
493
- callback();
501
+ resolve();
502
+ });
494
503
  });
495
- }, callback);
504
+ };
505
+
506
+ if (callback) {
507
+ executor().then(result => callback(null, result)).catch(callback);
508
+ } else {
509
+ return executor();
510
+ }
496
511
  },
497
- unlock: function(key, callback) {
498
- callback = callback || function() {};
499
- redisClient.del(key, callback);
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.0",
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.3.0"
33
+ "version": "3.4.0"
34
34
  }