jxp 2.13.2 → 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,6 +1,18 @@
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;
3
+
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
+ }
4
16
 
5
17
  const generateKey = (req, res) => {
6
18
  if (res.jxp_cache_key) return res.jxp_cache_key;
@@ -16,40 +28,41 @@ const generateKey = (req, res) => {
16
28
  }
17
29
 
18
30
  const set = async (req, res) => {
19
- if (!config.cache?.enabled) return;
31
+ if (!req.config || !req.config.cache.enabled) return;
20
32
  const key = generateKey(req, res)
21
33
  cache.set(key, res.result)
22
- if (config.cache?.debug) {
34
+ if (!req.config.cache.debug) {
23
35
  console.log('cache set', key)
24
36
  }
25
37
  }
26
38
 
27
39
  const get = (req, res, next) => {
28
- if (!config.cache?.enabled) return;
40
+ if (!req.config || !req.config.cache.enabled) return next();
29
41
  const key = generateKey(req, res)
30
42
  res.header('jxp-cache-key', key);
31
43
  const cached = cache.get(key)
32
44
  if (cached) {
33
- if (config.cache?.debug) {
45
+ if (!req.config.cache.debug) {
34
46
  console.log('cache hit', key)
35
47
  }
36
48
  res.header('jxp-cache', 'hit')
37
49
  res.result = cached
38
- return res.send(res.result)
50
+ res.send(res.result);
51
+ return;
39
52
  }
40
- if (config.cache?.debug) {
53
+ if (!req.config.cache.debug) {
41
54
  console.log('cache miss', key)
42
55
  }
43
56
  res.header('jxp-cache', 'miss')
44
57
  next()
45
58
  }
46
59
 
47
- const clear = async (req, res) => {
48
- if (!config.cache?.enabled) return;
60
+ const clear = async (req) => {
61
+ if (!req.config || !req.config.cache.enabled) return;
49
62
  const keys = cache.keys()
50
63
  keys.forEach(key => {
51
64
  if (key.startsWith(`${req.modelname}/`)) {
52
- if (config.cache?.debug) {
65
+ if (!req.config.cache.debug) {
53
66
  console.log('cache del', key)
54
67
  }
55
68
  cache.del(key)
@@ -58,15 +71,11 @@ const clear = async (req, res) => {
58
71
  }
59
72
 
60
73
  const clearAll = async () => {
61
- if (!config.cache?.enabled) return;
62
- if (config.cache?.debug) {
63
- console.log('cache flushAll')
64
- }
65
74
  cache.flushAll()
66
75
  }
67
76
 
68
77
  const stats = async (req, res) => {
69
- if (!config.cache?.enabled) {
78
+ if (!req.config || !req.config.cache.enabled) {
70
79
  res.result = { cache_enabled: false }
71
80
  return;
72
81
  }
@@ -74,6 +83,7 @@ const stats = async (req, res) => {
74
83
  }
75
84
 
76
85
  module.exports = {
86
+ init,
77
87
  set,
78
88
  get,
79
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
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.2",
4
+ "version": "2.13.3",
5
5
  "private": false,
6
6
  "main": "libs/jxp.js",
7
7
  "scripts": {