jxp 2.13.1 → 2.13.3

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/libs/cache.js CHANGED
@@ -1,8 +1,21 @@
1
1
  const NodeCache = require('node-cache');
2
- const config = require('config');
3
- const cache = new NodeCache({ stdTTL: config.cache?.ttl || 5 * 60 });
2
+ let cache;
4
3
 
5
- const generateKey = (req) => {
4
+ const init = (config) => {
5
+ if (!config.cache || !config.cache.enabled) return;
6
+ cache = new NodeCache({ stdTTL: config.cache.ttl || 5 * 60 });
7
+ if (config.cache.debug) {
8
+ cache.on('expired', (key) => {
9
+ console.log('cache expired', key)
10
+ })
11
+ cache.on('flush', () => {
12
+ console.log('cache flush')
13
+ })
14
+ }
15
+ }
16
+
17
+ const generateKey = (req, res) => {
18
+ if (res.jxp_cache_key) return res.jxp_cache_key;
6
19
  let key = `${req.modelname}/`;
7
20
  if (req.params.item_id) {
8
21
  key += `${req.params.item_id}`
@@ -10,43 +23,46 @@ const generateKey = (req) => {
10
23
  if (req.query) {
11
24
  key += `?${JSON.stringify(req.query)}`
12
25
  }
26
+ res.jxp_cache_key = key;
13
27
  return key;
14
28
  }
15
29
 
16
30
  const set = async (req, res) => {
17
- if (!config.cache?.enabled) return;
18
- const key = generateKey(req)
31
+ if (!req.config || !req.config.cache.enabled) return;
32
+ const key = generateKey(req, res)
19
33
  cache.set(key, res.result)
20
- if (config.cache?.debug) {
34
+ if (!req.config.cache.debug) {
21
35
  console.log('cache set', key)
22
36
  }
23
37
  }
24
38
 
25
- const get = async (req, res) => {
26
- if (!config.cache?.enabled) return;
27
- const key = generateKey(req)
39
+ const get = (req, res, next) => {
40
+ if (!req.config || !req.config.cache.enabled) return next();
41
+ const key = generateKey(req, res)
28
42
  res.header('jxp-cache-key', key);
29
43
  const cached = cache.get(key)
30
44
  if (cached) {
31
- if (config.cache?.debug) {
45
+ if (!req.config.cache.debug) {
32
46
  console.log('cache hit', key)
33
47
  }
34
48
  res.header('jxp-cache', 'hit')
35
49
  res.result = cached
36
- return res.send(res.result)
50
+ res.send(res.result);
51
+ return;
37
52
  }
38
- if (config.cache?.debug) {
53
+ if (!req.config.cache.debug) {
39
54
  console.log('cache miss', key)
40
55
  }
41
56
  res.header('jxp-cache', 'miss')
57
+ next()
42
58
  }
43
59
 
44
- const clear = async (req, res) => {
45
- if (!config.cache?.enabled) return;
60
+ const clear = async (req) => {
61
+ if (!req.config || !req.config.cache.enabled) return;
46
62
  const keys = cache.keys()
47
63
  keys.forEach(key => {
48
64
  if (key.startsWith(`${req.modelname}/`)) {
49
- if (config.cache?.debug) {
65
+ if (!req.config.cache.debug) {
50
66
  console.log('cache del', key)
51
67
  }
52
68
  cache.del(key)
@@ -55,19 +71,19 @@ const clear = async (req, res) => {
55
71
  }
56
72
 
57
73
  const clearAll = async () => {
58
- if (!config.cache?.enabled) return;
59
- if (config.cache?.debug) {
60
- console.log('cache flushAll')
61
- }
62
74
  cache.flushAll()
63
75
  }
64
76
 
65
77
  const stats = async (req, res) => {
66
- if (!config.cache?.enabled) return {};
78
+ if (!req.config || !req.config.cache.enabled) {
79
+ res.result = { cache_enabled: false }
80
+ return;
81
+ }
67
82
  res.result = cache.getStats()
68
83
  }
69
84
 
70
85
  module.exports = {
86
+ init,
71
87
  set,
72
88
  get,
73
89
  clear,
package/libs/jxp.js CHANGED
@@ -857,6 +857,7 @@ const JXP = function(options) {
857
857
  login.init(config);
858
858
  groups.init(config);
859
859
  ws.init({models});
860
+ cache.init(config);
860
861
  const docs = new Docs({config, models});
861
862
 
862
863
  // Set up our API server
@@ -932,7 +933,7 @@ const JXP = function(options) {
932
933
  middlewarePasswords,
933
934
  config.pre_hooks.post,
934
935
  actionPost,
935
- cache.clear,
936
+ cache.clearAll,
936
937
  (req, res, next) => {
937
938
  next();
938
939
  },
@@ -946,7 +947,7 @@ const JXP = function(options) {
946
947
  middlewareCheckAdmin,
947
948
  config.pre_hooks.put,
948
949
  actionPut,
949
- cache.clear,
950
+ cache.clearAll,
950
951
  (req, res, next) => {
951
952
  next();
952
953
  },
@@ -958,7 +959,7 @@ const JXP = function(options) {
958
959
  security.auth,
959
960
  config.pre_hooks.delete,
960
961
  actionDelete,
961
- cache.clear,
962
+ cache.clearAll,
962
963
  );
963
964
 
964
965
  // Count
@@ -1022,7 +1023,7 @@ const JXP = function(options) {
1022
1023
  middlewareCheckAdmin,
1023
1024
  config.pre_hooks.update,
1024
1025
  actionUpdate,
1025
- cache.clear,
1026
+ cache.clearAll,
1026
1027
  );
1027
1028
 
1028
1029
  /* Batch routes - ROLLED BACK FOR NOW */
@@ -1035,7 +1036,7 @@ const JXP = function(options) {
1035
1036
  security.login,
1036
1037
  security.auth,
1037
1038
  actionCall,
1038
- cache.clear,
1039
+ cache.clearAll,
1039
1040
  );
1040
1041
  server.post(
1041
1042
  "/call/:modelname/:method_name",
@@ -1043,7 +1044,7 @@ const JXP = function(options) {
1043
1044
  security.login,
1044
1045
  security.auth,
1045
1046
  actionCall,
1046
- cache.clear,
1047
+ cache.clearAll,
1047
1048
  );
1048
1049
  server.get(
1049
1050
  "/call/:modelname/:item_id/:method_name",
@@ -1051,7 +1052,7 @@ const JXP = function(options) {
1051
1052
  security.login,
1052
1053
  security.auth,
1053
1054
  actionCallItem,
1054
- cache.clear,
1055
+ cache.clearAll,
1055
1056
  );
1056
1057
 
1057
1058
  /* Login and authentication */
@@ -1101,7 +1102,7 @@ const JXP = function(options) {
1101
1102
 
1102
1103
  /* Cache */
1103
1104
  server.get("/cache/stats", cache.stats, outputJSON);
1104
- server.get("/cache/clear", cache.clear, outputJSON);
1105
+ server.get("/cache/clear", cache.clearAll, outputJSON);
1105
1106
 
1106
1107
  return server;
1107
1108
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jxp",
3
3
  "description:": "An opinionated RESTful API library based on Mongoose and Restify. Make an API by just writing Mongoose models.",
4
- "version": "2.13.1",
4
+ "version": "2.13.3",
5
5
  "private": false,
6
6
  "main": "libs/jxp.js",
7
7
  "scripts": {
package/test/test.js CHANGED
@@ -1142,7 +1142,7 @@ describe('Test', () => {
1142
1142
  chai.request(server)
1143
1143
  .get("/cache/clear")
1144
1144
  .end((err, res) => {
1145
- console.log(res.body);
1145
+ // console.log(res.body);
1146
1146
  res.should.have.status(200);
1147
1147
  done();
1148
1148
  });
@@ -1181,4 +1181,116 @@ describe('Test', () => {
1181
1181
  });
1182
1182
  })
1183
1183
  });
1184
+ describe("Cache invalidating", () => {
1185
+ let test_id;
1186
+ it("should get a test record", done => {
1187
+ chai.request(server)
1188
+ .get(`/api/test?populate=link&limit=1`)
1189
+ .auth(init.email, init.password)
1190
+ .end((err, res) => {
1191
+ // console.log(res.headers)
1192
+ res.should.have.status(200);
1193
+ res.body.data[0].should.have.property("_id");
1194
+ test_id = res.body.data[0]._id;
1195
+ res.body.data[0].should.have.property("link");
1196
+ should.equal(res.body.data[0].link, null);
1197
+ res.headers["jxp-cache"].should.equal("miss");
1198
+ done();
1199
+ });
1200
+ });
1201
+ it("should get a cached test record", done => {
1202
+ chai.request(server)
1203
+ .get(`/api/test?populate=link&limit=1`)
1204
+ .auth(init.email, init.password)
1205
+ .end((err, res) => {
1206
+ // console.log(res.headers)
1207
+ res.should.have.status(200);
1208
+ res.body.data[0].should.have.property("_id");
1209
+ res.headers["jxp-cache"].should.equal("hit");
1210
+ should.equal(res.body.data[0].link, null);
1211
+ done();
1212
+ });
1213
+ });
1214
+ let link_id;
1215
+ it("should add a link record", done => {
1216
+ chai.request(server)
1217
+ .post(`/api/link`)
1218
+ .auth(init.email, init.password)
1219
+ .send({
1220
+ name: "cache_test",
1221
+ val: "YoYoYo"
1222
+ })
1223
+ .end((err, res) => {
1224
+ // console.log(res.body)
1225
+ res.should.have.status(200);
1226
+ res.body.data.should.have.property("_id");
1227
+ link_id = res.body.data._id;
1228
+ done();
1229
+ });
1230
+ });
1231
+ it("should put link_id into test record", done => {
1232
+ chai.request(server)
1233
+ .put(`/api/test/${test_id}`)
1234
+ .auth(init.admin_email, init.admin_password)
1235
+ .send({
1236
+ link_id
1237
+ })
1238
+ .end((err, res) => {
1239
+ // console.log(res.body)
1240
+ res.should.have.status(200);
1241
+ res.body.data.should.have.property("_id");
1242
+ res.body.data.link_id.should.equal(link_id);
1243
+ done();
1244
+ });
1245
+ });
1246
+ it("should get a test record with link", done => {
1247
+ chai.request(server)
1248
+ .get(`/api/test?populate=link&limit=1`)
1249
+ .auth(init.email, init.password)
1250
+ .end((err, res) => {
1251
+ // console.log(res.body)
1252
+ res.should.have.status(200);
1253
+ res.body.data[0].should.have.property("_id");
1254
+ res.body.data[0].should.have.property("link");
1255
+ res.body.data[0].link.should.not.equal(null);
1256
+ res.body.data[0].link._id.should.equal(link_id);
1257
+ res.body.data[0].link.name.should.equal("cache_test");
1258
+ res.body.data[0].link.val.should.equal("YoYoYo");
1259
+ res.headers["jxp-cache"].should.equal("miss");
1260
+ done();
1261
+ });
1262
+ });
1263
+ it("should update link record", done => {
1264
+ chai.request(server)
1265
+ .put(`/api/link/${link_id}`)
1266
+ .auth(init.admin_email, init.admin_password)
1267
+ .send({
1268
+ val: "YoYoYo2"
1269
+ })
1270
+ .end((err, res) => {
1271
+ // console.log(res.body)
1272
+ res.should.have.status(200);
1273
+ res.body.data.should.have.property("_id");
1274
+ res.body.data.val.should.equal("YoYoYo2");
1275
+ done();
1276
+ });
1277
+ });
1278
+ it("should get a test record with updated link", done => {
1279
+ chai.request(server)
1280
+ .get(`/api/test?populate=link&limit=1`)
1281
+ .auth(init.email, init.password)
1282
+ .end((err, res) => {
1283
+ // console.log(res.body.data[0].link)
1284
+ res.should.have.status(200);
1285
+ res.body.data[0].should.have.property("_id");
1286
+ res.body.data[0].should.have.property("link");
1287
+ res.body.data[0].link.should.not.equal(null);
1288
+ res.body.data[0].link._id.should.equal(link_id);
1289
+ res.body.data[0].link.name.should.equal("cache_test");
1290
+ res.body.data[0].link.val.should.equal("YoYoYo2");
1291
+ res.headers["jxp-cache"].should.equal("miss");
1292
+ done();
1293
+ });
1294
+ });
1295
+ })
1184
1296
  });