petty-cache 3.1.0 → 3.3.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 CHANGED
@@ -6,6 +6,14 @@ 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
+
13
+ ## [3.2.0] - 2021-04-05
14
+ ### Changed
15
+ - Upgraded `redis` version to `~3.1.0`.
16
+
9
17
  ## [3.1.0] - 2021-02-16
10
18
  ### Added
11
19
  - Added the ability for `pettyCache.bulkFetch` to specify TTL options.
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
@@ -1,3 +1,5 @@
1
+ const util = require('util');
2
+
1
3
  const async = require('async');
2
4
  const lock = require('lock').Lock();
3
5
  const memoryCache = require('memory-cache');
@@ -322,7 +324,7 @@ function PettyCache() {
322
324
  }
323
325
 
324
326
  // Try to get value from Redis
325
- getFromRedis(key, function(err, result) {
327
+ getFromRedis(key, async function(err, result) {
326
328
  if (err) {
327
329
  return callback(err);
328
330
  }
@@ -334,15 +336,27 @@ function PettyCache() {
334
336
  }
335
337
 
336
338
  // 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);
339
+ if (util.types.isAsyncFunction(func)) {
340
+ try {
341
+ const data = await func();
342
+
343
+ _this.set(key, data, options, function(err) {
344
+ callback(err, data);
345
+ });
346
+ } catch(err) {
347
+ callback(err);
340
348
  }
341
-
342
- _this.set(key, data, options, function(err) {
343
- callback(err, data);
349
+ } else {
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
@@ -4,12 +4,12 @@
4
4
  "async": "~3.2.0",
5
5
  "lock": "~1.1.0",
6
6
  "memory-cache": "~0.2.0",
7
- "redis": "~3.0.2"
7
+ "redis": "~3.1.0"
8
8
  },
9
9
  "devDependencies": {
10
10
  "coveralls": "*",
11
- "nyc": "*",
12
- "mocha": "*"
11
+ "mocha": "*",
12
+ "nyc": "*"
13
13
  },
14
14
  "homepage": "https://github.com/mediocre/petty-cache",
15
15
  "keywords": [
@@ -30,5 +30,5 @@
30
30
  "type": "git",
31
31
  "url": "https://github.com/mediocre/petty-cache.git"
32
32
  },
33
- "version": "3.1.0"
33
+ "version": "3.3.0"
34
34
  }