exframe-cache-manager 1.1.3 → 1.3.1

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/.eslintignore CHANGED
@@ -1,5 +1,5 @@
1
- node_modules
2
- test
3
- documentation
4
- dist
5
- debug.js
1
+ node_modules
2
+ test
3
+ documentation
4
+ dist
5
+ debug.js
package/.eslintrc.json CHANGED
@@ -1,24 +1,17 @@
1
- {
2
- "env": {
3
- "es6": true,
4
- "node": true
5
- },
6
- "extends": "airbnb-base",
7
- "rules": {
8
- "comma-dangle": [2, "never"],
9
- "consistent-return": [0],
10
- "arrow-body-style": [1, "as-needed"],
11
- "import/no-unresolved": [0],
12
- "strict": [0, "global"],
13
- "linebreak-style": ["error", "windows"],
14
- "indent": 0,
15
- "max-len": ["error", 200],
16
- "no-param-reassign": ["error", { "props": false }],
17
- "no-else-return": 0,
18
- "eol-last": 0,
19
- "no-useless-escape": 0,
20
- "no-underscore-dangle": 0,
21
- "no-fallthrough": 0,
22
- "arrow-parens": "off"
23
- }
24
- }
1
+ {
2
+ "env": {
3
+ "es6": true,
4
+ "node": true
5
+ },
6
+ "extends": "airbnb-base",
7
+ "rules": {
8
+ "comma-dangle": ["error", "never"],
9
+ "strict": ["off", "global"] ,
10
+ "consistent-return": "off",
11
+ "linebreak-style": ["error", "windows"],
12
+ "max-len": ["error", 200],
13
+ "no-param-reassign": ["error", { "props": false }],
14
+ "no-underscore-dangle": "off",
15
+ "arrow-parens": "off"
16
+ }
17
+ }
package/Dockerfile CHANGED
@@ -1,17 +1,17 @@
1
- FROM node:8-alpine
2
-
3
- LABEL maintainer=Exzeo
4
-
5
- RUN apk update && \
6
- apk --no-cache add bash libc6-compat && \
7
- addgroup -S docker && adduser -S -G docker docker
8
-
9
- COPY ./package.json /app/package.json
10
- WORKDIR /app
11
- RUN npm install && \
12
- npm cache clean --force
13
-
14
- COPY . /app
15
-
16
- # Override the command, to run the test instead of the application
17
- CMD ["npm", "run", "unit-test"]
1
+ FROM node:14-alpine
2
+
3
+ LABEL maintainer=Exzeo
4
+
5
+ RUN apk update && \
6
+ apk --no-cache add bash libc6-compat && \
7
+ addgroup -S docker && adduser -S -G docker docker
8
+
9
+ COPY ./package.json /app/package.json
10
+ WORKDIR /app
11
+ RUN npm install && \
12
+ npm cache clean --force
13
+
14
+ COPY . /app
15
+
16
+ # Override the command, to run the test instead of the application
17
+ CMD ["npm", "run", "unit-test"]
package/README.md CHANGED
@@ -1,2 +1,2 @@
1
- # exframe-cache-manager
2
-
1
+ # exframe-cache-manager
2
+
@@ -1,18 +1,17 @@
1
- version: "2"
2
- services:
3
- exframe-cache-manager:
4
- container_name: exframe-cache-manager
5
- build:
6
- context: .
7
- volumes:
8
- - .:/app
9
- - /app/node_modules
10
- depends_on:
11
- - "redis"
12
- environment:
13
- - "HOST=redis"
14
- redis:
15
- image: "redis"
16
- container_name: Redis
17
- ports:
1
+ version: "2"
2
+ services:
3
+ exframe-cache-manager:
4
+ container_name: exframe-cache-manager
5
+ build:
6
+ context: .
7
+ volumes:
8
+ - .:/app
9
+ depends_on:
10
+ - "redis"
11
+ environment:
12
+ - "HOST=redis"
13
+ redis:
14
+ image: "redis"
15
+ container_name: Redis
16
+ ports:
18
17
  - "6379:6379"
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
-
4
3
  const CacheManager = require('cache-manager');
5
4
  const RedisStore = require('cache-manager-redis');
6
5
  const util = require('util');
6
+ const { lazyInstrument } = require('exframe-metrics');
7
7
 
8
8
  const db = 0;
9
9
 
@@ -33,7 +33,7 @@ function cachemanager(options) {
33
33
  const cacheStorePool = Object.assign(redisStore._pool, { db });
34
34
  cacheStorePool.setMaxListeners(0);
35
35
 
36
- return {
36
+ const instance = {
37
37
  /**
38
38
  *
39
39
  * @param {string} key
@@ -95,7 +95,7 @@ function cachemanager(options) {
95
95
  * @returns {Promise<true>}
96
96
  */
97
97
  async healthCheck() {
98
- return await this.processRedisCommands(client => client && client.server_info.loading === '0');
98
+ return this.processRedisCommands(client => client && client.server_info.loading === '0');
99
99
  },
100
100
 
101
101
  close() {
@@ -168,16 +168,23 @@ function cachemanager(options) {
168
168
  context.log.info('matched keys', {
169
169
  keys: result[1]
170
170
  });
171
- if (keys.length <= 0) {
172
- return;
171
+ if (keys.length > 0) {
172
+ // eslint-disable-next-line no-await-in-loop
173
+ await delAsync(...keys);
173
174
  }
174
- // eslint-disable-next-line no-await-in-loop
175
- await delAsync(...keys);
176
175
  } while (cursor !== '0');
177
176
  });
178
177
  },
179
178
  cacheStorePool
180
179
  };
180
+
181
+ Object.keys(instance).forEach((key) => {
182
+ if (typeof instance[key] === 'function') {
183
+ instance[key] = lazyInstrument(instance[key].bind(instance), { metricPrefix: 'cache' });
184
+ }
185
+ });
186
+
187
+ return instance;
181
188
  }
182
189
 
183
190
  module.exports = {
package/package.json CHANGED
@@ -1,33 +1,49 @@
1
- {
2
- "name": "exframe-cache-manager",
3
- "version": "1.1.3",
4
- "description": "Managing the cache",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "docker-compose -f docker-compose.yml up --abort-on-container-exit --exit-code-from exframe-cache-manager",
8
- "unit-test": "istanbul cover --dir ./documentation/coverage node_modules/mocha/bin/_mocha \"./test/**/*.test.js\""
9
- },
10
- "author": "",
11
- "license": "ISC",
12
- "dependencies": {
13
- "@types/cache-manager": "^1.2.8",
14
- "@types/redis": "^2.8.13",
15
- "assert": "^1.4.1",
16
- "cache-manager": "^2.9.0",
17
- "cache-manager-redis": "^0.6.0"
18
- },
19
- "devDependencies": {
20
- "chai": "^4.2.0",
21
- "cross-env": "^3.2.4",
22
- "eslint": "^3.1.1",
23
- "eslint-config-airbnb-base": "^5.0.0",
24
- "eslint-plugin-import": "^1.12.0",
25
- "istanbul": "^1.1.0-alpha.1",
26
- "loadtest": "^2.3.0",
27
- "mocha": "^3.5.3",
28
- "mocha-teamcity-reporter": "^2.2.1",
29
- "nodemon": "^1.18.7",
30
- "sinon": "^2.4.1",
31
- "sinon-mongoose": "^2.0.2"
32
- }
33
- }
1
+ {
2
+ "name": "exframe-cache-manager",
3
+ "version": "1.3.1",
4
+ "description": "Managing the cache",
5
+ "main": "index.js",
6
+ "config": {
7
+ "reporter": "mocha-exzeo-reporter"
8
+ },
9
+ "mocha": {
10
+ "recursive": true
11
+ },
12
+ "scripts": {
13
+ "pretest": "npm-install-peers && npm run lint",
14
+ "lint": "./node_modules/eslint/bin/eslint.js .",
15
+ "test": "docker-compose -f docker-compose.yml up --abort-on-container-exit --exit-code-from exframe-cache-manager",
16
+ "unit-test": "nyc -r lcov --report-dir ./documentation/coverage -t ./documentation/coverage mocha --reporter $npm_package_config_reporter \"./test/**/*.test.js\""
17
+ },
18
+ "author": "Exzeo",
19
+ "license": "ISC",
20
+ "peerDependencies": {
21
+ "exframe-health": "*",
22
+ "exframe-logger": "*",
23
+ "exframe-service": "*"
24
+ },
25
+ "dependencies": {
26
+ "@types/cache-manager": "^3.4.2",
27
+ "@types/redis": "^2.8.32",
28
+ "cache-manager": "^3.6.0",
29
+ "cache-manager-redis": "^0.6.0",
30
+ "exframe-metrics": "^1.1.0",
31
+ "redis": "^3.1.2"
32
+ },
33
+ "devDependencies": {
34
+ "chai": "^4.3.4",
35
+ "eslint": "^8.3.0",
36
+ "eslint-config-airbnb-base": "^15.0.0",
37
+ "eslint-plugin-import": "^2.25.3",
38
+ "mocha": "*",
39
+ "mocha-exzeo-reporter": "0.0.3",
40
+ "npm-install-peers": "*",
41
+ "nyc": "*"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "https://bitbucket.org/exzeo-usa/exframe",
46
+ "directory": "packages/exframe-cache-manager"
47
+ },
48
+ "gitHead": "8a3a00cd3f3111be2cf9d3c6487de58d6b880000"
49
+ }
@@ -0,0 +1,21 @@
1
+ # The language the application is written in.
2
+ ; sonar.language=js
3
+ sonar.language=JavaScript
4
+
5
+ # Path is relative to the sonar-project.properties file. Defaults to .
6
+ sonar.sources=.
7
+
8
+ # # Encoding of the source code. Default is default system encoding
9
+ sonar.sourceEncoding=UTF-8
10
+
11
+ # # Patterns used to exclude some source files from analysis.
12
+ sonar.exclusions=test*/**/*.js
13
+
14
+ # # Patterns used to include some test files and only these ones in analysis.
15
+ sonar.test.inclusions=test*/**/*.js
16
+
17
+ # Patterns used to exclude some files from coverage report.
18
+ sonar.coverage.exclusions=test*/**/*.js,node_modules/*,documentation/coverage/lcov-report/*,junit/*
19
+
20
+ # Paths (absolute or relative) to the files with LCOV data.
21
+ sonar.javascript.lcov.reportPaths=documentation/coverage/lcov.info
@@ -1,124 +1,136 @@
1
- 'use strict';
2
-
3
- const sinon = require('sinon');
4
- const expect = require('chai').expect;
5
- const cacheManager = require('../index');
6
-
7
- const userId = 'auth0|1234567890';
8
- const cacheKey = `${userId}_profiletest`;
9
-
10
- const options = {
11
- "host": process.env.HOST || "localhost",
12
- "port": process.env.PORT || 6379,
13
- "auth_pass": "",
14
- "db": 0,
15
- "ttl": 60
16
- };
17
-
18
- const userProfileResponse = {
19
- userId: userId,
20
- userName: 'CSRMickey',
21
- email: 'CMM@discsr.com',
22
- profile: {
23
- family_name: 'Mickey',
24
- given_name: ' Mouse CSR'
25
- },
26
- resources: [{
27
- uri: 'TTIC:FL:HO3:CSR1',
28
- right: 'READ'
29
- }, {
30
- uri: 'TTIC:FL:HO3:CSR2',
31
- right: 'WRITE'
32
- }]
33
- };
34
-
35
- context('Test User Profile Middleware', () => {
36
-
37
- beforeEach(() => {
38
- this.sinon = sinon.sandbox.create();
39
- });
40
-
41
- afterEach(() => {
42
- this.sinon.restore();
43
- });
44
-
45
- describe('Cache Manager', () => {
46
- const app = cacheManager.create(options);
47
-
48
- it('validate the setItem function on the basis of key and value', async () => {
49
- app.setItem(cacheKey, "value1").then((data)=>{
50
- expect(data).to.not.eql(null);
51
- expect(data).to.eql("value1");
52
- })
53
- await app.flushCache({log:{info:(info)=>{console.log(info)}}}, cacheKey);
54
- });
55
-
56
- it('validate the getItem function on the basis of key', async () => {
57
- await app.setItem(cacheKey,"someValue");
58
- const value = await app.getItem(cacheKey);
59
- expect(value).to.eql("someValue");
60
- await app.flushCache({log:{info:(info)=>{console.log(info)}}}, cacheKey);
61
- });
62
-
63
- it('validate the addSetItem function on the basis of key', async () => {
64
- const key = 'SetTestKey';
65
- await app.addSetItem(key, "someSetValue");
66
- const value = await app.isSetMember(key, "someSetValue");
67
- expect(value).to.eql(true);
68
- await app.flushCache({log:{info:(info)=>{console.log(info)}}}, key);
69
- });
70
-
71
- it('validate the isSetMember function on the basis of key', async () => {
72
- const key = 'SetTestKey2';
73
- await app.addSetItem(key, "someSetValue");
74
- const value1 = await app.isSetMember(key, "someSetValue");
75
- expect(value1).to.eql(true);
76
- const value2 = await app.isSetMember(key, "notSetValue");
77
- expect(value2).to.eql(false, 'expected non-member key to return false for isSetMember');
78
- await app.flushCache({log:{info:(info)=>{console.log(info)}}}, key);
79
- });
80
-
81
- it('validate the removeSetMember function on the basis of key', async () => {
82
- const key = 'SetTestKey3';
83
- await app.addSetItem(key, "someSetValue");
84
- let value = await app.isSetMember(key, "someSetValue");
85
- expect(value).to.eql(true);
86
- await app.removeSetItem(key, "someSetValue");
87
- value = await app.isSetMember(key, "someSetValue");
88
- expect(value).to.eql(false, 'expected non-member key to return false for isSetMember');
89
- await app.flushCache({log:{info:(info)=>{console.log(info)}}}, key);
90
- });
91
-
92
- it('close drains the redis pool', async () => {
93
- const app1 = cacheManager.create(options);
94
- await app1.setItem(cacheKey,"someValue");
95
- const value = await app1.getItem(cacheKey);
96
- expect(value).to.eql("someValue");
97
- await app1.close();
98
-
99
- try {
100
- const result = await app1.getItem(cacheKey);
101
- expect(result).to.not.be.ok();
102
- } catch (ex) {
103
- expect(ex.message).to.be.equal('pool is draining and cannot accept work');
104
- }
105
- });
106
-
107
- it('validate the flushCache function', async () => {
108
- await app.flushCache({log:{info:(info)=>{console.log(info)}}}, '*_profiletest');
109
- const value = await app.getItem(cacheKey);
110
- expect(value).to.eql(null);
111
- });
112
-
113
- it('validate the cacheStorePool', () => {
114
- const catchepool = app.cacheStorePool;
115
- expect(app).to.not.eql(null);
116
- expect(catchepool.db).to.eql(0);
117
- expect(catchepool._maxListeners).to.eql(0);
118
- });
119
- it('validate the healthCheck function', async () => {
120
- const healthCheck = await app.healthCheck();
121
- expect(healthCheck).to.eql(true);
122
- });
123
- });
1
+ 'use strict';
2
+
3
+ const expect = require('chai').expect;
4
+ const { prometheusClient } = require('exframe-service');
5
+ const cacheManager = require('../index');
6
+
7
+ const userId = 'auth0|1234567890';
8
+ const cacheKey = `${userId}_profiletest`;
9
+
10
+ const options = {
11
+ "host": process.env.HOST || "localhost",
12
+ "port": process.env.PORT || 6379,
13
+ "auth_pass": "",
14
+ "db": 0,
15
+ "ttl": 60
16
+ };
17
+
18
+ const userProfileResponse = {
19
+ userId: userId,
20
+ userName: 'CSRMickey',
21
+ email: 'CMM@discsr.com',
22
+ profile: {
23
+ family_name: 'Mickey',
24
+ given_name: ' Mouse CSR'
25
+ },
26
+ resources: [{
27
+ uri: 'TTIC:FL:HO3:CSR1',
28
+ right: 'READ'
29
+ }, {
30
+ uri: 'TTIC:FL:HO3:CSR2',
31
+ right: 'WRITE'
32
+ }]
33
+ };
34
+
35
+ context('Test User Profile Middleware', () => {
36
+ describe('Cache Manager', () => {
37
+ const app = cacheManager.create(options);
38
+
39
+ beforeEach(() => {
40
+ prometheusClient.register.clear();
41
+ expect(prometheusClient.register.getMetricsAsArray()).to.have.lengthOf(0);
42
+ });
43
+
44
+ it('validate the setItem function on the basis of key and value', async () => {
45
+ const data = await app.setItem(cacheKey, "value1");
46
+ expect(data).to.not.eql(null);
47
+ expect(data).to.eql("value1");
48
+ const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('setItem'));
49
+ expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
50
+ await app.flushCache({log:{info:(info)=>{console.log(info)}}}, cacheKey);
51
+ });
52
+
53
+ it('validate the getItem function on the basis of key', async () => {
54
+ await app.setItem(cacheKey,"someValue");
55
+ const value = await app.getItem(cacheKey);
56
+ expect(value).to.eql("someValue");
57
+ const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('getItem'));
58
+ expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
59
+ await app.flushCache({log:{info:(info)=>{console.log(info)}}}, cacheKey);
60
+ });
61
+
62
+ it('validate the addSetItem function on the basis of key', async () => {
63
+ const key = 'SetTestKey';
64
+ await app.addSetItem(key, "someSetValue");
65
+ const value = await app.isSetMember(key, "someSetValue");
66
+ expect(value).to.eql(true);
67
+ const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('addSetItem'));
68
+ expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
69
+ await app.flushCache({log:{info:(info)=>{console.log(info)}}}, key);
70
+ });
71
+
72
+ it('validate the isSetMember function on the basis of key', async () => {
73
+ const key = 'SetTestKey2';
74
+ await app.addSetItem(key, "someSetValue");
75
+ const value1 = await app.isSetMember(key, "someSetValue");
76
+ expect(value1).to.eql(true);
77
+ const value2 = await app.isSetMember(key, "notSetValue");
78
+ expect(value2).to.eql(false, 'expected non-member key to return false for isSetMember');
79
+ const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('isSetMember'));
80
+ expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
81
+ await app.flushCache({log:{info:(info)=>{console.log(info)}}}, key);
82
+ });
83
+
84
+ it('validate the removeSetMember function on the basis of key', async () => {
85
+ const key = 'SetTestKey3';
86
+ await app.addSetItem(key, "someSetValue");
87
+ let value = await app.isSetMember(key, "someSetValue");
88
+ expect(value).to.eql(true);
89
+ await app.removeSetItem(key, "someSetValue");
90
+ value = await app.isSetMember(key, "someSetValue");
91
+ expect(value).to.eql(false, 'expected non-member key to return false for isSetMember');
92
+ const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('removeSetItem'));
93
+ expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
94
+ await app.flushCache({log:{info:(info)=>{console.log(info)}}}, key);
95
+ });
96
+
97
+ it('close drains the redis pool', async () => {
98
+ const app1 = cacheManager.create(options);
99
+ await app1.setItem(cacheKey,"someValue");
100
+ const value = await app1.getItem(cacheKey);
101
+ expect(value).to.eql("someValue");
102
+ await app1.close();
103
+
104
+ try {
105
+ const result = await app1.getItem(cacheKey);
106
+ expect(result).to.not.be.ok();
107
+ } catch (ex) {
108
+ expect(ex.message).to.be.equal('pool is draining and cannot accept work');
109
+ const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('close'));
110
+ expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
111
+ }
112
+ });
113
+
114
+ it('validate the flushCache function', async () => {
115
+ await app.flushCache({log:{info:(info)=>{console.log(info)}}}, '*_profiletest');
116
+ const value = await app.getItem(cacheKey);
117
+ expect(value).to.eql(null);
118
+ const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('flushCache'));
119
+ expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
120
+ });
121
+
122
+ it('validate the cacheStorePool', () => {
123
+ const catchepool = app.cacheStorePool;
124
+ expect(app).to.not.eql(null);
125
+ expect(catchepool.db).to.eql(0);
126
+ expect(catchepool._maxListeners).to.eql(0);
127
+ });
128
+
129
+ it('validate the healthCheck function', async () => {
130
+ const healthCheck = await app.healthCheck();
131
+ expect(healthCheck).to.eql(true);
132
+ const metrics = prometheusClient.register.getMetricsAsArray().filter((m) => m.name.includes('healthCheck'));
133
+ expect(metrics).to.be.an('array').and.to.have.lengthOf.above(0);
134
+ });
135
+ });
124
136
  });