exframe-cache-manager 1.3.3 → 2.0.2

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
@@ -7,11 +7,11 @@ RUN apk update && \
7
7
  addgroup -S docker && adduser -S -G docker docker
8
8
 
9
9
  COPY ./package.json /app/package.json
10
- WORKDIR /app
11
- RUN npm install && \
12
- npm cache clean --force
13
-
14
10
  COPY . /app
15
11
 
12
+ WORKDIR /app
13
+ RUN yarn install
14
+ RUN npm run peers
15
+
16
16
  # Override the command, to run the test instead of the application
17
17
  CMD ["npm", "run", "unit-test"]
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const CacheManager = require('cache-manager');
4
- const RedisStore = require('cache-manager-redis');
4
+ const RedisStore = require('cache-manager-redis-store');
5
5
  const util = require('util');
6
6
  const { lazyInstrument } = require('exframe-metrics');
7
7
 
@@ -27,13 +27,25 @@ function cachemanager(options) {
27
27
 
28
28
  // @ts-ignore
29
29
  const redisStore = redisCache.store;
30
+ const redisClient = redisStore.getClient();
31
+ redisClient.setMaxListeners(0);
30
32
 
31
- // need to expose the store's connection pool after it's created by the cache manager as it does not seem to have a way to pass it in
32
- // if we use something besides redis, we'll have to tackle this differently
33
- const cacheStorePool = Object.assign(redisStore._pool, { db });
34
- cacheStorePool.setMaxListeners(0);
33
+ redisClient.on('error', (e) => {
34
+ console.log('Unhandled Redis Error', { errorDetails: e }); // eslint-disable-line
35
+ });
36
+
37
+ process.on('beforeExit', () => {
38
+ if (redisClient.connected) {
39
+ redisClient.removeAllListeners();
40
+ redisClient.end(true);
41
+ }
42
+ });
35
43
 
36
44
  const instance = {
45
+ get cacheStorePool() {
46
+ return redisClient;
47
+ },
48
+
37
49
  /**
38
50
  *
39
51
  * @param {string} key
@@ -100,33 +112,20 @@ function cachemanager(options) {
100
112
 
101
113
  close() {
102
114
  return new Promise(res => {
103
- cacheStorePool.drain(res);
115
+ redisClient.removeAllListeners();
116
+ redisClient.end(true);
117
+ res();
104
118
  });
105
119
  },
106
120
 
107
121
  getClient() {
108
122
  return new Promise((resolve, reject) => {
109
- cacheStorePool.acquireDb(async (connError, client) => {
110
- let timeout;
111
-
112
- if (connError) {
113
- return reject(connError);
114
- }
115
-
116
- if (client.connected) {
117
- return resolve(client);
118
- }
119
-
120
- client.once('ready', () => {
121
- clearTimeout(timeout);
122
- resolve(client);
123
- });
123
+ if (redisClient.connected) {
124
+ return resolve(redisClient);
125
+ }
124
126
 
125
- timeout = setTimeout(() => {
126
- cacheStorePool.release(client);
127
- reject(new Error('Timeout waiting for redis client connection'));
128
- }, 10000);
129
- }, db);
127
+ redisClient.on('ready', () => resolve(redisClient));
128
+ redisClient.on('error', (e) => reject(e));
130
129
  });
131
130
  },
132
131
 
@@ -135,17 +134,8 @@ function cachemanager(options) {
135
134
  * @param {(client: any) => any} fn
136
135
  */
137
136
  async processRedisCommands(fn) {
138
- let client;
139
- let result;
140
-
141
- try {
142
- client = await this.getClient();
143
- result = await fn(client);
144
- } finally {
145
- if (client) {
146
- cacheStorePool.release(client);
147
- }
148
- }
137
+ const client = await this.getClient();
138
+ const result = await fn(client);
149
139
 
150
140
  return result;
151
141
  },
@@ -173,7 +163,7 @@ function cachemanager(options) {
173
163
  } while (cursor !== '0');
174
164
  });
175
165
  },
176
- cacheStorePool
166
+ redisClient
177
167
  };
178
168
 
179
169
  Object.keys(instance).forEach((key) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exframe-cache-manager",
3
- "version": "1.3.3",
3
+ "version": "2.0.2",
4
4
  "description": "Managing the cache",
5
5
  "main": "index.js",
6
6
  "config": {
@@ -12,8 +12,9 @@
12
12
  "scripts": {
13
13
  "pretest": "npm-install-peers && npm run lint",
14
14
  "lint": "eslint ./",
15
+ "peers": "npm-install-peers",
15
16
  "test": "docker-compose -f docker-compose.yml up --abort-on-container-exit --exit-code-from exframe-cache-manager",
16
- "unit-test": "./node_modules/.bin/nyc -r lcov --report-dir ./documentation/coverage -t ./documentation/coverage mocha --reporter $npm_package_config_reporter \"./test/**/*.test.js\""
17
+ "unit-test": "./node_modules/.bin/nyc -r lcov --report-dir ./documentation/coverage -t ./documentation/coverage mocha --exit --reporter $npm_package_config_reporter \"./test/**/*.test.js\""
17
18
  },
18
19
  "author": "Exzeo",
19
20
  "license": "ISC",
@@ -24,26 +25,25 @@
24
25
  },
25
26
  "dependencies": {
26
27
  "@types/cache-manager": "^3.4.2",
27
- "@types/redis": "^2.8.32",
28
28
  "cache-manager": "^3.6.0",
29
- "cache-manager-redis": "^0.6.0",
30
- "exframe-metrics": "^1.1.0",
31
- "redis": "^3.1.2"
29
+ "cache-manager-redis-store": "^2.0.0",
30
+ "exframe-metrics": "^1.1.0"
32
31
  },
33
32
  "devDependencies": {
34
- "chai": "^4.3.4",
35
- "eslint": "^8.3.0",
33
+ "chai": "*",
34
+ "eslint": "*",
36
35
  "eslint-config-exzeo": "*",
37
- "eslint-plugin-import": "^2.25.3",
36
+ "eslint-plugin-import": "*",
38
37
  "mocha": "*",
39
- "mocha-exzeo-reporter": "0.0.3",
38
+ "mocha-exzeo-reporter": "*",
40
39
  "npm-install-peers": "*",
41
- "nyc": "*"
40
+ "nyc": "*",
41
+ "sinon": "*"
42
42
  },
43
43
  "repository": {
44
44
  "type": "git",
45
45
  "url": "https://bitbucket.org/exzeo-usa/exframe",
46
46
  "directory": "packages/exframe-cache-manager"
47
47
  },
48
- "gitHead": "ef87c842aa08c54cbcb03ee075db8463ebc8fd43"
48
+ "gitHead": "3ff1cd2d1bc50184f5aae8d1ea35e04e1cb718b2"
49
49
  }
@@ -105,7 +105,7 @@ context('Test User Profile Middleware', () => {
105
105
  const result = await app1.getItem(cacheKey);
106
106
  expect(result).to.not.be.ok();
107
107
  } catch (ex) {
108
- expect(ex.message).to.be.equal('pool is draining and cannot accept work');
108
+ expect(ex.message).to.be.equal('GET can\'t be processed. The connection is already closed.');
109
109
  const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('close'));
110
110
  expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
111
111
  }
@@ -122,7 +122,6 @@ context('Test User Profile Middleware', () => {
122
122
  it('validate the cacheStorePool', () => {
123
123
  const catchepool = app.cacheStorePool;
124
124
  expect(app).to.not.eql(null);
125
- expect(catchepool.db).to.eql(0);
126
125
  expect(catchepool._maxListeners).to.eql(0);
127
126
  });
128
127