cache account info in redis db

merge-requests/208/head
some random guy 4 years ago
parent cd789dba0c
commit 5dc3fe9504
  1. 47
      db/accounts.js
  2. 1
      gulpfile.js
  3. 7
      helpers/sessionrefresh.js

@ -2,7 +2,8 @@
const Mongo = require(__dirname+'/db.js') const Mongo = require(__dirname+'/db.js')
, db = Mongo.client.db('jschan').collection('accounts') , db = Mongo.client.db('jschan').collection('accounts')
, bcrypt = require('bcrypt'); , bcrypt = require('bcrypt')
, cache = require(__dirname+'/../redis.js');
module.exports = { module.exports = {
@ -32,7 +33,7 @@ module.exports = {
// hash the password // hash the password
const passwordHash = await bcrypt.hash(password, 12); const passwordHash = await bcrypt.hash(password, 12);
//add to db //add to db
return db.insertOne({ const res = await db.insertOne({
'_id': username, '_id': username,
original, original,
authLevel, authLevel,
@ -40,17 +41,21 @@ module.exports = {
'ownedBoards': [], 'ownedBoards': [],
'modBoards': [] 'modBoards': []
}); });
cache.del(`users:${username}`);
return res;
}, },
changePassword: async (username, newPassword) => { changePassword: async (username, newPassword) => {
const passwordHash = await bcrypt.hash(newPassword, 12); const passwordHash = await bcrypt.hash(newPassword, 12);
return db.updateOne({ const res = await db.updateOne({
'_id': username '_id': username
}, { }, {
'$set': { '$set': {
'passwordHash': passwordHash 'passwordHash': passwordHash
} }
}); });
cache.del(`users:${username}`);
return res;
}, },
find: (filter, skip=0, limit=0) => { find: (filter, skip=0, limit=0) => {
@ -63,36 +68,42 @@ module.exports = {
}).skip(skip).limit(limit).toArray(); }).skip(skip).limit(limit).toArray();
}, },
deleteMany: (usernames) => { deleteMany: async (usernames) => {
return db.deleteMany({ const res = await db.deleteMany({
'_id': { '_id': {
'$in': usernames '$in': usernames
} }
}); });
cache.del(usernames.map(n => `users:${n}`));
return res;
}, },
addOwnedBoard: (username, board) => { addOwnedBoard: async (username, board) => {
return db.updateOne({ const res = await db.updateOne({
'_id': username '_id': username
}, { }, {
'$addToSet': { '$addToSet': {
'ownedBoards': board 'ownedBoards': board
} }
}); });
cache.del(`users:${username}`);
return res;
}, },
removeOwnedBoard: (username, board) => { removeOwnedBoard: async (username, board) => {
return db.updateOne({ const res = await db.updateOne({
'_id': username '_id': username
}, { }, {
'$pull': { '$pull': {
'ownedBoards': board 'ownedBoards': board
} }
}); });
cache.del(`users:${username}`);
return res;
}, },
addModBoard: (usernames, board) => { addModBoard: async (usernames, board) => {
return db.updateMany({ const res = await db.updateMany({
'_id': { '_id': {
'$in': usernames '$in': usernames
} }
@ -101,10 +112,12 @@ module.exports = {
'modBoards': board 'modBoards': board
} }
}); });
cache.del(`users:${username}`);
return res;
}, },
removeModBoard: (usernames, board) => { removeModBoard: async (usernames, board) => {
return db.updateMany({ const res = await db.updateMany({
'_id': { '_id': {
'$in': usernames '$in': usernames
} }
@ -113,6 +126,8 @@ module.exports = {
'modBoards': board 'modBoards': board
} }
}); });
cache.del(`users:${username}`);
return res;
}, },
getOwnedOrModBoards: (usernames) => { getOwnedOrModBoards: (usernames) => {
@ -140,9 +155,9 @@ module.exports = {
}).toArray(); }).toArray();
}, },
setLevel: (usernames, level) => { setLevel: async (usernames, level) => {
//increase users auth level //increase users auth level
return db.updateMany({ const res = await db.updateMany({
'_id': { '_id': {
'$in': usernames '$in': usernames
} }
@ -151,6 +166,8 @@ module.exports = {
'authLevel': level 'authLevel': level
} }
}); });
cache.del(`users:${username}`);
return res;
}, },
deleteAll: () => { deleteAll: () => {

@ -164,6 +164,7 @@ async function cache() {
Redis.deletePattern('boards:listed'), Redis.deletePattern('boards:listed'),
Redis.deletePattern('board:*'), Redis.deletePattern('board:*'),
Redis.deletePattern('banners:*'), Redis.deletePattern('banners:*'),
Redis.deletePattern('users:*'),
Redis.deletePattern('blacklisted:*'), Redis.deletePattern('blacklisted:*'),
]); ]);
Redis.redisClient.quit(); Redis.redisClient.quit();

@ -1,10 +1,10 @@
'use strict'; 'use strict';
const { Accounts } = require(__dirname+'/../db/'); const { Accounts } = require(__dirname+'/../db/')
, cache = require(__dirname+'/../redis.js');
module.exports = async (req, res, next) => { module.exports = async (req, res, next) => {
if (req.session && req.session.user) { if (req.session && req.session.user && !res.locals.user) {
// keeping session updated incase user updated on global manage
const account = await Accounts.findOne(req.session.user); const account = await Accounts.findOne(req.session.user);
if (!account) { if (!account) {
req.session.destroy(); req.session.destroy();
@ -15,6 +15,7 @@ module.exports = async (req, res, next) => {
'modBoards': account.modBoards, 'modBoards': account.modBoards,
'ownedBoards': account.ownedBoards, 'ownedBoards': account.ownedBoards,
}; };
cache.set(`users:${req.session.user}`, res.locals.user, 3600);
} }
} }
next(); next();

Loading…
Cancel
Save