exframe-cache-manager 2.0.5 → 2.0.7

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 CHANGED
@@ -10,7 +10,7 @@ COPY ./package.json /app/package.json
10
10
  COPY . /app
11
11
 
12
12
  WORKDIR /app
13
- RUN yarn install
13
+ RUN npm install
14
14
  RUN npm run peers
15
15
 
16
16
  # Override the command, to run the test instead of the application
package/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
+ const util = require('util');
4
+ const { setTimeout: waitTimeout } = require('timers/promises');
3
5
  const CacheManager = require('cache-manager');
4
6
  const RedisStore = require('cache-manager-redis-store');
5
- const util = require('util');
6
7
  const { lazyInstrument } = require('exframe-metrics');
7
8
  const health = require('exframe-health');
8
9
  const service = require('exframe-service');
9
10
  const { generateShortId } = require('exframe-utilities');
10
- const { setTimeout: waitTimeout } = require('timers/promises');
11
11
 
12
12
  const db = 0;
13
13
 
@@ -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
- getItem(key) {
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.5",
3
+ "version": "2.0.7",
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": "9b41242e2d499ea24109ab29d51c76652ee03f43"
49
+ "gitHead": "d9641658b6e0f058726402b7fe6a859cfd4b11ab"
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');