用expressjs构建管理面板,这是无需查询很多即可计数用户的最佳方法吗? [英] Building admin panel in expressjs, best way to count users without querying a lot?
问题描述
因此,我正在为我的应用构建管理面板。在管理员的主页上,我想显示一些有关使用情况的统计信息。
So I'm building an admin panel for my app. On the homepage of admin, I want to show some statistics about the usage.
示例:24小时,7天,30天,全天候注册
注册方法:用户/密码,facebook,twitter等
Examples: signups in 24 hours, 7 days, 30 days, all time signup method: user/pass, facebook, twitter etc
现在在我的 admin.index
路由器中,对我的数据库执行异步调用(使用mongoose的mongodb)获取所有计数,并在最终的回调函数中呈现页面。
Now in my admin.index
router, I'm doing async calls to my db (mongodb using mongoose) to get all the counts, and in the final callback function, rendering the page.
exports.index = function(req, res){
async.parallel({
// https://github.com/caolan/async#parallel
// counts number of users in date ranges
all_time: function(next) {
console.log('querying');
User.count({}, function (err, count) {
if (err) return next(err);
next(null, count);
});
},
past_day: function(next) {
console.log('querying');
User.count({'created_at': {$gte: yesterday}}, function(err, count) {
if (err) return next(err);
next(null, count);
});
},
week: function(next) {
console.log('querying');
User.count({'created_at': {$gte: week}}, function(err, count) {
if (err) return next(err);
next(null, count);
});
},
month: function(next) {
console.log('querying');
User.count({'created_at': {$gte: month}}, function(err, count) {
if (err) return next(err);
next(null, count);
});
},
// Counts number of user signups for each strategy
local: function(next) {
console.log('querying');
User.count({'strategy': 'local'}, function(err, count) {
if (err) return next(err);
next(null, count);
});
},
google: function(next) {
console.log('querying');
User.count({'strategy': 'google'}, function(err, count) {
if (err) return next(err);
next(null, count);
});
},
facebook: function(next) {
console.log('querying');
User.count({'strategy': 'facebook'}, function(err, count) {
if (err) return next(err);
next(null, count);
});
},
twitter: function(next) {
console.log('querying');
User.count({'strategy': 'twitter'}, function(err, count) {
if (err) return next(err);
next(null, count);
});
}
},
function(err, r) {
if (err) throw err;
res.render('admin/index', {
appName: Constants.APP_NAME,
title: 'Admin',
total_users: r.all_time,
yesterday: r.past_day,
week: r.week,
month: r.month,
local: r.local,
google: r.google,
facebook: r.facebook,
twitter: r.twitter
})
}
);
}
但是现在,我正在进行计算(8个查询)每次加载页面时。有什么更好的方法呢?我从未构建过CRUD应用程序,因此了解的不多。我也没有Express和Node的经验,所以也不知道它在这里如何工作。
But as it is right now, I am doing the computation (8 queries) on every page load. What's a better way to do it? I have never built CRUD applications, so do not know much. I have no experience with express and node, too so don't know much how it would work here.
我应该使用内存缓存,还是过大?
Should I use memcached, or is it an overkill? How would that work with async?
推荐答案
首先,最好在可能的时候进行优化,看看 $ or运算符。
也许您只能使用一个请求,具体取决于您的数据结构。
如果不能这样做,则可以只用计数创建文档,查询该文档,然后在一个小的请求中获得所有计数。
First, it is good to optimize when you can, take a look at the $or operator. Maybe you will can just use one request, depend on your data structure. If you can't, you can make a document with only the counts, querying this doc and you get all counts in a small request.
但是我认为这里没有必要进行优化。这些查询是针对管理员的。假设您有1位管理员和1万名用户连接到您的网站,那么这些查询不是针对用户,而是仅针对您有时会使用的查询;
But I think this optimization here isn't necessary. These queries are for administrators. Imagine you've 1 administrator and 10 000 users connected into your website, these query aren't for users, only for you, that you use sometimes; the impact is trivial.
此外,您的查询受益于异步的事实,因此您的应用程序可以在同一时间响应其他用户。
Furthermore, your queries benefit from the fact that there are asynchrone, so your app can respond other users during the same time.
这篇关于用expressjs构建管理面板,这是无需查询很多即可计数用户的最佳方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!