petty-cache 3.2.0 → 3.3.1

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 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.3.0] - 2024-07-03
10
+ ### Changed
11
+ - Added the ability for `pettyCache.fetch` to support async functions.
12
+
9
13
  ## [3.2.0] - 2021-04-05
10
14
  ### Changed
11
15
  - Upgraded `redis` version to `~3.1.0`.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # petty-cache
2
2
 
3
- [![Build Status](https://github.com/mediocre/petty-cache/workflows/build/badge.svg)](https://github.com/mediocre/petty-cache/actions?query=workflow%3Abuild)
4
- [![Coverage Status](https://coveralls.io/repos/github/mediocre/petty-cache/badge.svg?branch=master)](https://coveralls.io/github/mediocre/petty-cache?branch=master)
3
+ [![Build Status](https://github.com/mediocre/petty-cache/actions/workflows/continuousIntegration.yaml/badge.svg?branch=main)](https://github.com/mediocre/petty-cache/actions?query=workflow%3Abuild+branch%3Amain)
4
+ [![Coverage Status](https://coveralls.io/repos/github/mediocre/petty-cache/badge.svg?branch=main)](https://coveralls.io/github/mediocre/petty-cache?branch=main)
5
5
 
6
6
  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 automatic serialization plus some extra features to avoid cache stampedes and thundering herds.
7
7
 
@@ -75,7 +75,7 @@ pettyCache.bulkFetch(['a', 'b', 'c', 'd'], function(keys, callback) {
75
75
 
76
76
  keys.forEach(function(key) {
77
77
  results[key] = key.toUpperCase();
78
- }
78
+ });
79
79
 
80
80
  callback(null, results);
81
81
  }, function(err, values) {
@@ -161,6 +161,16 @@ pettyCache.fetch('key', function(callback) {
161
161
  });
162
162
  ```
163
163
 
164
+ ```javascript
165
+ pettyCache.fetch('key', async () => {
166
+ // This function is called on a cache miss
167
+ return await fs.readFile('file.txt');
168
+ }, function(err, value) {
169
+ // This callback is called once petty-cache has loaded data from cache or executed the specified cache miss function
170
+ console.log(value);
171
+ });
172
+ ```
173
+
164
174
  **Options**
165
175
 
166
176
  ```
package/index.js CHANGED
@@ -322,7 +322,7 @@ function PettyCache() {
322
322
  }
323
323
 
324
324
  // Try to get value from Redis
325
- getFromRedis(key, function(err, result) {
325
+ getFromRedis(key, async function(err, result) {
326
326
  if (err) {
327
327
  return callback(err);
328
328
  }
@@ -334,15 +334,29 @@ function PettyCache() {
334
334
  }
335
335
 
336
336
  // Execute the specified function and place the results in cache before returning the data
337
- func(function(err, data) {
338
- if (err) {
339
- return callback(err);
337
+ if (func.length === 0) {
338
+ // If the function doesn't have any arguments, there wasn't a callback provided
339
+ try {
340
+ const data = await func();
341
+
342
+ _this.set(key, data, options, function(err) {
343
+ callback(err, data);
344
+ });
345
+ } catch(err) {
346
+ callback(err);
340
347
  }
341
-
342
- _this.set(key, data, options, function(err) {
343
- callback(err, data);
348
+ } else {
349
+ // If the function has arguments, there was a callback provided
350
+ func(function(err, data) {
351
+ if (err) {
352
+ return callback(err);
353
+ }
354
+
355
+ _this.set(key, data, options, function(err) {
356
+ callback(err, data);
357
+ });
344
358
  });
345
- });
359
+ }
346
360
  });
347
361
  })(releaseRedisLock(function(err, result) {
348
362
  if (result.error) {
@@ -406,7 +420,7 @@ function PettyCache() {
406
420
 
407
421
  this.get = function(key, callback) {
408
422
  // Try to get value from memory cache
409
- var result = getFromMemoryCache(key);
423
+ let result = getFromMemoryCache(key);
410
424
 
411
425
  // Return value from memory cache if it exists
412
426
  if (result.exists) {
package/package.json CHANGED
@@ -30,5 +30,5 @@
30
30
  "type": "git",
31
31
  "url": "https://github.com/mediocre/petty-cache.git"
32
32
  },
33
- "version": "3.2.0"
33
+ "version": "3.3.1"
34
34
  }