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
app/services/PolyUserServ.js
1 | -var PolyUserModl = require('../models/PolyUserModl'); | |
2 | 1 | var LineTransform = require('node-line-reader').LineTransform; |
3 | 2 | var fs = require('fs'); |
4 | 3 | var Client = require('ssh2').Client; |
5 | 4 | var creds = require('../../config/sshAuth'); |
5 | +var NodeCache = require("node-cache"); | |
6 | + | |
7 | +var cache = new NodeCache(); | |
6 | 8 | |
7 | 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 | 18 | PolyUserServ.readPasswd = function (login, cb) { |
10 | 19 | passwdF = 'config/passwd'; |
11 | 20 | fs.exists(passwdF, function (exists) { |
... | ... | @@ -82,11 +91,6 @@ PolyUserServ.grabInfos = function (login, cb) { |
82 | 91 | nom: passwd.GECOS, |
83 | 92 | section: group.name.toUpperCase() |
84 | 93 | }); |
85 | - PolyUserModl.create({ | |
86 | - login: login, | |
87 | - nom: passwd.GECOS, | |
88 | - section: group.name.toUpperCase() | |
89 | - }); | |
90 | 94 | } else { |
91 | 95 | if (group === undefined) { |
92 | 96 | console.error("Impossible d'ouvrir le fichier des groupes."); |
... | ... | @@ -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 | 123 | PolyUserServ.add = function (login, cb) { |
140 | 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 | 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 | 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 | }; | ... | ... |