banner rotating uses srandmember from redis of board banners

merge-requests/208/head
fatchan 5 years ago
parent 69b0b484c0
commit 5ef7da7100
  1. 27
      db/boards.js
  2. 31
      models/pages/randombanner.js
  3. 14
      redis.js

@ -9,21 +9,32 @@ module.exports = {
db,
findOne: async (name) => {
const cacheKey = `board_${name}`;
let board = await cache.get(cacheKey);
let board = await cache.get(`board_${name}`);
if (board && board !== 'no_exist') {
return board;
} else {
board = await db.findOne({ '_id': name });
if (board) {
cache.set(cacheKey, board);
cache.set(`board_${name}`, board);
cache.sadd(`banners_${name}`, board.banners);
} else {
cache.set(cacheKey, 'no_exist', 'ex', 3600); //1 hour expiry just so it doesnt grow indefinitely
cache.set(`board_${name}`, 'no_exist', 'ex', 3600); //1 hour expiry just so it doesnt grow indefinitely
}
}
return board;
},
randomBanner: async (name) => {
let banner = await cache.srand(`banners_${name}`);
if (!banner) {
const board = await module.exports.findOne(name);
if (board) {
banner = board.banners[Math.floor(Math.random()*board.banners.length)];
}
}
return banner;
},
setOwner: (board, username) => {
cache.del(`board_${board}`);
return db.updateOne({
@ -46,15 +57,21 @@ module.exports = {
deleteOne: (board) => {
cache.del(`board_${board}`);
cache.del(`banners_${board}`);
return db.deleteOne({ '_id': board });
},
deleteAll: (board) => {
/*
no clearing redis cache here, will leave that up to gulpfile, since this happens in the
wipe script, it would delete redis cache for everything, not just boards
*/
return db.deleteMany({});
},
removeBanners: (board, filenames) => {
cache.del(`board_${board}`);
cache.del(`banners_${board}`);
return db.updateOne(
{
'_id': board,
@ -68,6 +85,7 @@ module.exports = {
addBanners: (board, filenames) => {
cache.del(`board_${board}`);
cache.del(`banners_${board}`);
return db.updateOne(
{
'_id': board,
@ -112,7 +130,6 @@ module.exports = {
},
getNextId: async (board) => {
//cache.del(`board_${board}`); //dont need to clear cache for this? only time this value is used, its fetched from db.
const increment = await db.findOneAndUpdate(
{
'_id': board

@ -1,6 +1,6 @@
'use strict';
const Boards = require(__dirname+'/../../db/boards.js');
const Boards = require(__dirname+'/../../db/boards.js')
module.exports = async (req, res, next) => {
@ -8,27 +8,18 @@ module.exports = async (req, res, next) => {
return next();
}
//agregate to get single random item from banners array
const board = await Boards.db.aggregate([
{
'$match': {
'_id': req.query.board
}
},
{
'$unwind': '$banners'
},
{
'$sample': {
'size' : 1
}
}
]).toArray().then(res => res[0]);
let banner;
try {
banner = await Boards.randomBanner(req.query.board);
} catch (err) {
return next(err);
}
if (board && board.banners != null) {
return res.redirect(`/banner/${req.query.board}/${board.banners}`);
if (!banner) {
//non existing boards will show default banner, but it doesnt really matter.
return res.redirect('/img/defaultbanner.png');
}
return res.redirect('/img/defaultbanner.png');
return res.redirect(`/banner/${req.query.board}/${banner}`);
}

@ -8,17 +8,29 @@ module.exports = {
redisClient: client,
//cache not used yet, but will need to JSON stringify things that are objects e.g. boards, threads
//get a value with key
get: async (key) => {
return client.get(key).then(res => {
return JSON.parse(res);
});
},
//set a value on key
set: (key, value) => {
return client.set(key, JSON.stringify(value));
},
//add items to a set
sadd: (key, value) => {
return client.sadd(key, value);
},
//get random item from set
srand: (key) => {
return client.srandmember(key);
},
//delete value with key
del: (key) => {
return client.del(key);
},

Loading…
Cancel
Save