exframe-cache-manager 2.0.4 → 2.0.6
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/Dockerfile +1 -1
- package/index.js +26 -2
- package/package.json +2 -2
- package/test/cacheManager.test.js +31 -0
package/Dockerfile
CHANGED
package/index.js
CHANGED
|
@@ -54,8 +54,31 @@ function cachemanager(options) {
|
|
|
54
54
|
/**
|
|
55
55
|
*
|
|
56
56
|
* @param {string} key
|
|
57
|
+
* @param {object} context
|
|
58
|
+
* @param {Options} options
|
|
57
59
|
*/
|
|
58
|
-
|
|
60
|
+
|
|
61
|
+
async getItem(context, key, { timeout } = {}) {
|
|
62
|
+
if (arguments.length === 1) {
|
|
63
|
+
// context = key, param order was switched in function def to preserve backwards compatability
|
|
64
|
+
return redisCache.get(context);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const controller = new AbortController();
|
|
68
|
+
if (timeout) {
|
|
69
|
+
const result = await Promise.race([
|
|
70
|
+
redisCache.get(key),
|
|
71
|
+
waitTimeout(timeout, false, { signal: controller.signal, ref: false })
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
if (!result) {
|
|
75
|
+
context.log.info('CACHE TIMEOUT ERROR');
|
|
76
|
+
return Promise.reject(new Error('Timed out attempting to retrieve item from cache.'));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
controller.abort();
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
59
82
|
return redisCache.get(key);
|
|
60
83
|
},
|
|
61
84
|
|
|
@@ -168,7 +191,8 @@ function cachemanager(options) {
|
|
|
168
191
|
} while (cursor !== '0');
|
|
169
192
|
});
|
|
170
193
|
},
|
|
171
|
-
redisClient
|
|
194
|
+
redisClient,
|
|
195
|
+
redisCache
|
|
172
196
|
};
|
|
173
197
|
|
|
174
198
|
health.add(`redis-${instance.id}`, async () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exframe-cache-manager",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
4
4
|
"description": "Managing the cache",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"config": {
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"url": "https://bitbucket.org/exzeo-usa/exframe",
|
|
47
47
|
"directory": "packages/exframe-cache-manager"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "0bb8e02e06b7b9b09afa070af552d341fa986fc7"
|
|
50
50
|
}
|
|
@@ -4,6 +4,7 @@ const { expect } = require('chai');
|
|
|
4
4
|
const sinon = require('sinon');
|
|
5
5
|
const service = require('exframe-service');
|
|
6
6
|
const health = require('exframe-health');
|
|
7
|
+
const { setTimeout: waitTimeout } = require('timers/promises');
|
|
7
8
|
const cacheManager = require('../index');
|
|
8
9
|
|
|
9
10
|
const { prometheusClient } = service;
|
|
@@ -77,6 +78,36 @@ context('Test User Profile Middleware', () => {
|
|
|
77
78
|
await app.flushCache({ log: { info: (info) => { console.log(info); } } }, cacheKey);
|
|
78
79
|
});
|
|
79
80
|
|
|
81
|
+
it('validate the getItem function on the basis of key, including context', async () => {
|
|
82
|
+
await app.setItem(cacheKey, 'someValue');
|
|
83
|
+
const context = { log: { info: (info) => { console.log(info); } } };
|
|
84
|
+
|
|
85
|
+
const value = await app.getItem(context, cacheKey);
|
|
86
|
+
expect(value).to.eql('someValue');
|
|
87
|
+
const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('getItem'));
|
|
88
|
+
expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
|
|
89
|
+
await app.flushCache({ log: { info: (info) => { console.log(info); } } }, cacheKey);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('validate the getItem function, including optional context and timeout delay value', async () => {
|
|
93
|
+
const fake = async function (key) {
|
|
94
|
+
await waitTimeout(5000)
|
|
95
|
+
return 'finished';
|
|
96
|
+
}
|
|
97
|
+
const stub = sinon.stub(app.redisCache, "get").callsFake(fake)
|
|
98
|
+
|
|
99
|
+
await app.setItem(cacheKey, 'someValue');
|
|
100
|
+
const context = { log: { info: (info) => { console.log(info); } } };
|
|
101
|
+
try {
|
|
102
|
+
const value = await app.getItem(context, cacheKey, { timeout: 250 });
|
|
103
|
+
expect(value).to.eql('Request should have timed out');
|
|
104
|
+
} catch (err) {
|
|
105
|
+
expect(err.message).to.eql('Timed out attempting to retrieve item from cache.');
|
|
106
|
+
}
|
|
107
|
+
await app.flushCache(context, cacheKey);
|
|
108
|
+
stub.restore();
|
|
109
|
+
});
|
|
110
|
+
|
|
80
111
|
it('validate the addSetItem function on the basis of key', async () => {
|
|
81
112
|
const key = 'SetTestKey';
|
|
82
113
|
await app.addSetItem(key, 'someSetValue');
|