petty-cache 3.2.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 +4 -0
- package/README.md +13 -3
- package/index.js +23 -9
- package/package.json +1 -1
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
|
-
[](https://github.com/mediocre/petty-cache/actions?query=workflow%3Abuild+branch%3Amain)
|
|
4
|
+
[](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
|
-
|
|
338
|
-
|
|
339
|
-
|
|
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
|
-
|
|
343
|
-
|
|
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
|
-
|
|
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