From 8bb44ece868294c10a433f3593bba01ab524e4a7 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sun, 12 Apr 2015 23:33:03 +0200 Subject: [PATCH] PolyUser est mis en cache correctement --- app/models/PolyUserModl.js | 16 ---------------- app/services/PolyUserServ.js | 62 ++++++++++++++++++++------------------------------------------ package.json | 1 + 3 files changed, 21 insertions(+), 58 deletions(-) delete mode 100644 app/models/PolyUserModl.js diff --git a/app/models/PolyUserModl.js b/app/models/PolyUserModl.js deleted file mode 100644 index 3ee9e88..0000000 --- a/app/models/PolyUserModl.js +++ /dev/null @@ -1,16 +0,0 @@ -var mongoose = require('mongoose'); - -module.exports = mongoose.model('PolyUser', { - login: { - type: String, - default: 'login' - }, - nom: { - type: String, - default: 'Nom' - }, - section: { - type: String, - default: '' - } -}); diff --git a/app/services/PolyUserServ.js b/app/services/PolyUserServ.js index 2bd70b8..43831c5 100644 --- a/app/services/PolyUserServ.js +++ b/app/services/PolyUserServ.js @@ -1,11 +1,20 @@ -var PolyUserModl = require('../models/PolyUserModl'); var LineTransform = require('node-line-reader').LineTransform; var fs = require('fs'); var Client = require('ssh2').Client; var creds = require('../../config/sshAuth'); +var NodeCache = require("node-cache"); + +var cache = new NodeCache(); var PolyUserServ = {}; +function isEmpty(obj) { + for (var i in obj) { + return false; + } + return true; +} + PolyUserServ.readPasswd = function (login, cb) { passwdF = 'config/passwd'; fs.exists(passwdF, function (exists) { @@ -82,11 +91,6 @@ PolyUserServ.grabInfos = function (login, cb) { nom: passwd.GECOS, section: group.name.toUpperCase() }); - PolyUserModl.create({ - login: login, - nom: passwd.GECOS, - section: group.name.toUpperCase() - }); } else { if (group === undefined) { console.error("Impossible d'ouvrir le fichier des groupes."); @@ -116,49 +120,23 @@ PolyUserServ.grabInfos = function (login, cb) { }); }; -PolyUserServ.addData = function (polyuser, cb) { - cb(null, polyuser); -}; - -PolyUserServ.obtain = function (login, cb) { - PolyUserModl.findOne({ - login: login - }, { - nom: 1, - section: 1, - _id: 0 - }, function (err, polyuser) { - if (err) { - cb(err); - } else { - PolyUserServ.addData(polyuser, cb); - } - }); -}; - PolyUserServ.add = function (login, cb) { PolyUserServ.grabInfos(login, function (data) { - data.login = login; - PolyUserModl.create(data, function (err, polyuser) { - PolyUserServ.obtain(login, cb); - }); + cb(null, data); + cache.set(login, data); }); }; -// PolyUserServ.remove = function (login, cb) { -// PolyUserModl.remove({ -// login: login -// }, cb); -// }; - PolyUserServ.get = function (login, cb) { - PolyUserModl.findOne({ - login: login - }, function (err, polyuser) { - if (polyuser && !err && polyuser.polyuser && polyuser.section) { - PolyUserServ.obtain(login, cb); + cache.get(login, function (err, data) { + if (err) { + cb(err); } else { - PolyUserServ.add(login, cb); + if (isEmpty(data)) { + PolyUserServ.add(login, cb); + } else { + cb(null, data[login]); + } } }); }; diff --git a/package.json b/package.json index 1cadc75..bf0d2e5 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "express": "^4.12.3", "express-http-proxy": "^0.5.0", "mongoose": "^4.0.1", + "node-cache": "^1.1.0", "node-line-reader": "0.0.2", "ssh2": "^0.4.6", "ursa": "^0.8.4" -- libgit2 0.21.2