Commit 8bb44ece868294c10a433f3593bba01ab524e4a7
1 parent
c1e8332d
PolyUser est mis en cache correctement
Showing
3 changed files
with
21 additions
and
58 deletions
Show diff stats
app/models/PolyUserModl.js deleted
@@ -1,16 +0,0 @@ | @@ -1,16 +0,0 @@ | ||
1 | -var mongoose = require('mongoose'); | ||
2 | - | ||
3 | -module.exports = mongoose.model('PolyUser', { | ||
4 | - login: { | ||
5 | - type: String, | ||
6 | - default: 'login' | ||
7 | - }, | ||
8 | - nom: { | ||
9 | - type: String, | ||
10 | - default: 'Nom' | ||
11 | - }, | ||
12 | - section: { | ||
13 | - type: String, | ||
14 | - default: '' | ||
15 | - } | ||
16 | -}); |
app/services/PolyUserServ.js
1 | -var PolyUserModl = require('../models/PolyUserModl'); | ||
2 | var LineTransform = require('node-line-reader').LineTransform; | 1 | var LineTransform = require('node-line-reader').LineTransform; |
3 | var fs = require('fs'); | 2 | var fs = require('fs'); |
4 | var Client = require('ssh2').Client; | 3 | var Client = require('ssh2').Client; |
5 | var creds = require('../../config/sshAuth'); | 4 | var creds = require('../../config/sshAuth'); |
5 | +var NodeCache = require("node-cache"); | ||
6 | + | ||
7 | +var cache = new NodeCache(); | ||
6 | 8 | ||
7 | var PolyUserServ = {}; | 9 | var PolyUserServ = {}; |
8 | 10 | ||
11 | +function isEmpty(obj) { | ||
12 | + for (var i in obj) { | ||
13 | + return false; | ||
14 | + } | ||
15 | + return true; | ||
16 | +} | ||
17 | + | ||
9 | PolyUserServ.readPasswd = function (login, cb) { | 18 | PolyUserServ.readPasswd = function (login, cb) { |
10 | passwdF = 'config/passwd'; | 19 | passwdF = 'config/passwd'; |
11 | fs.exists(passwdF, function (exists) { | 20 | fs.exists(passwdF, function (exists) { |
@@ -82,11 +91,6 @@ PolyUserServ.grabInfos = function (login, cb) { | @@ -82,11 +91,6 @@ PolyUserServ.grabInfos = function (login, cb) { | ||
82 | nom: passwd.GECOS, | 91 | nom: passwd.GECOS, |
83 | section: group.name.toUpperCase() | 92 | section: group.name.toUpperCase() |
84 | }); | 93 | }); |
85 | - PolyUserModl.create({ | ||
86 | - login: login, | ||
87 | - nom: passwd.GECOS, | ||
88 | - section: group.name.toUpperCase() | ||
89 | - }); | ||
90 | } else { | 94 | } else { |
91 | if (group === undefined) { | 95 | if (group === undefined) { |
92 | console.error("Impossible d'ouvrir le fichier des groupes."); | 96 | console.error("Impossible d'ouvrir le fichier des groupes."); |
@@ -116,49 +120,23 @@ PolyUserServ.grabInfos = function (login, cb) { | @@ -116,49 +120,23 @@ PolyUserServ.grabInfos = function (login, cb) { | ||
116 | }); | 120 | }); |
117 | }; | 121 | }; |
118 | 122 | ||
119 | -PolyUserServ.addData = function (polyuser, cb) { | ||
120 | - cb(null, polyuser); | ||
121 | -}; | ||
122 | - | ||
123 | -PolyUserServ.obtain = function (login, cb) { | ||
124 | - PolyUserModl.findOne({ | ||
125 | - login: login | ||
126 | - }, { | ||
127 | - nom: 1, | ||
128 | - section: 1, | ||
129 | - _id: 0 | ||
130 | - }, function (err, polyuser) { | ||
131 | - if (err) { | ||
132 | - cb(err); | ||
133 | - } else { | ||
134 | - PolyUserServ.addData(polyuser, cb); | ||
135 | - } | ||
136 | - }); | ||
137 | -}; | ||
138 | - | ||
139 | PolyUserServ.add = function (login, cb) { | 123 | PolyUserServ.add = function (login, cb) { |
140 | PolyUserServ.grabInfos(login, function (data) { | 124 | PolyUserServ.grabInfos(login, function (data) { |
141 | - data.login = login; | ||
142 | - PolyUserModl.create(data, function (err, polyuser) { | ||
143 | - PolyUserServ.obtain(login, cb); | ||
144 | - }); | 125 | + cb(null, data); |
126 | + cache.set(login, data); | ||
145 | }); | 127 | }); |
146 | }; | 128 | }; |
147 | 129 | ||
148 | -// PolyUserServ.remove = function (login, cb) { | ||
149 | -// PolyUserModl.remove({ | ||
150 | -// login: login | ||
151 | -// }, cb); | ||
152 | -// }; | ||
153 | - | ||
154 | PolyUserServ.get = function (login, cb) { | 130 | PolyUserServ.get = function (login, cb) { |
155 | - PolyUserModl.findOne({ | ||
156 | - login: login | ||
157 | - }, function (err, polyuser) { | ||
158 | - if (polyuser && !err && polyuser.polyuser && polyuser.section) { | ||
159 | - PolyUserServ.obtain(login, cb); | 131 | + cache.get(login, function (err, data) { |
132 | + if (err) { | ||
133 | + cb(err); | ||
160 | } else { | 134 | } else { |
161 | - PolyUserServ.add(login, cb); | 135 | + if (isEmpty(data)) { |
136 | + PolyUserServ.add(login, cb); | ||
137 | + } else { | ||
138 | + cb(null, data[login]); | ||
139 | + } | ||
162 | } | 140 | } |
163 | }); | 141 | }); |
164 | }; | 142 | }; |
package.json
@@ -9,6 +9,7 @@ | @@ -9,6 +9,7 @@ | ||
9 | "express": "^4.12.3", | 9 | "express": "^4.12.3", |
10 | "express-http-proxy": "^0.5.0", | 10 | "express-http-proxy": "^0.5.0", |
11 | "mongoose": "^4.0.1", | 11 | "mongoose": "^4.0.1", |
12 | + "node-cache": "^1.1.0", | ||
12 | "node-line-reader": "0.0.2", | 13 | "node-line-reader": "0.0.2", |
13 | "ssh2": "^0.4.6", | 14 | "ssh2": "^0.4.6", |
14 | "ursa": "^0.8.4" | 15 | "ursa": "^0.8.4" |