exframe-cache-manager 1.3.3 → 2.0.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/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,14 @@ function cachemanager(options) {
27
27
 
28
28
  // @ts-ignore
29
29
  const redisStore = redisCache.store;
30
-
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);
30
+ const redisClient = redisStore.getClient();
31
+ redisClient.setMaxListeners(0);
35
32
 
36
33
  const instance = {
34
+ get cacheStorePool() {
35
+ return redisClient;
36
+ },
37
+
37
38
  /**
38
39
  *
39
40
  * @param {string} key
@@ -100,33 +101,19 @@ function cachemanager(options) {
100
101
 
101
102
  close() {
102
103
  return new Promise(res => {
103
- cacheStorePool.drain(res);
104
+ redisClient.end(true);
105
+ res();
104
106
  });
105
107
  },
106
108
 
107
109
  getClient() {
108
110
  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
- });
111
+ if (redisClient.connected) {
112
+ return resolve(redisClient);
113
+ }
124
114
 
125
- timeout = setTimeout(() => {
126
- cacheStorePool.release(client);
127
- reject(new Error('Timeout waiting for redis client connection'));
128
- }, 10000);
129
- }, db);
115
+ redisClient.on('ready', () => resolve(redisClient));
116
+ redisClient.on('error', (e) => reject(e));
130
117
  });
131
118
  },
132
119
 
@@ -135,17 +122,8 @@ function cachemanager(options) {
135
122
  * @param {(client: any) => any} fn
136
123
  */
137
124
  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
- }
125
+ const client = await this.getClient();
126
+ const result = await fn(client);
149
127
 
150
128
  return result;
151
129
  },
@@ -173,7 +151,7 @@ function cachemanager(options) {
173
151
  } while (cursor !== '0');
174
152
  });
175
153
  },
176
- cacheStorePool
154
+ redisClient
177
155
  };
178
156
 
179
157
  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.0",
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": "93013f9bb8a75217a24b69b7c2ad72b8baebddc7"
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