如何优化实时应用程序的数据库查询 - Nodejs
How to optimize querying to database for real-time apps - Nodejs
我在 Nodejs 中创建了一个实时应用程序,我正在使用 socket.io 向给定频道中的所有连接用户广播数据。例如,我正在对 API-url 进行 http 调用:每 1 秒 http://example.com/api/token=tokenumber
并获取新的 json 数据。这些数据在推送到客户端查看之前需要做一个算术运算(为了进行算术运算,应用程序需要连接到数据库,如:mongo 或 mysql 并获取将要执行的数据用于计算)。
例如-工作流程
step 1) make http call [received data x=80, y=90]
step 2) connect to db [received data a=10, b=20]
step 3) do calculation [finalval = x*a + y*b]
step 4) push data to view
在这里,我们连接到数据库,因为那些 a 和 b 变量 是动态的,由管理员使用某种控制面板设置。
问题
目前,我每 1 秒连接一次数据库,因为我每 1 秒进行一次 http 调用。但我很确定 a 和 b 的值将是相同的,直到任何管理员用户登录到控制面板并更改这些值。 简而言之,资源浪费,我相信我通过每 1 秒连接到数据库并获得相同的未更改值来大量使用资源
我该如何优化这个?我如何确定数据库 table 中是否发生了任何更改,如果发生了任何更改,我如何获取新值并使用某种内存缓存存储它,以便节省时间和通过在值未更改时不连接到数据库来获取资源。
需要注意的一件事是,当数据库中的值发生变化时 table,它们应该立即反映出来,因为应用程序是实时的。
我知道每 1 分钟或 5 分钟检查一次数据库 table 的解决方案,以查明值是否已更改,具体取决于缓存的新值。但这意味着如果我在 t0 更改 a 和 b 的值,最坏的情况是在 1 或 5 分钟后更新视图,一旦执行到 db,我不希望这样。
更新
所以,我有我之前解释过的代码:我正在尝试将从 db 中提取的值保存到某个变量 X。但是在下一次调用时,它 被设置为未定义,因为函数测试是每 N 秒调用一次。但与数据库的连接只发生一次.
我只会在应用程序运行时调用数据库,每当管理员对数据库进行任何更改并点击提交按钮时我都会再次调用它。
我不想进行任何数据库调用,但在此之前使用从 Db 中提取的相同值。我尝试将它存储在一个变量中,但下一次调用发生时,同一个变量被设置为未定义。
代码
//requiring model products
var Product = require('./models/product');
//Here function is an async callback
Product.find(function (err, products) {
if (err) return console.error(err);
console.log("callback function results: "+products);
//I am passing data to test function once I receive the data from MongoDB
test(products);
});
//This function is called every N seconds
function test(val) {
//Here I am trying to store mongoDB data to variable hello
var hello = val;
//This prints only once when data from mongodb is recieved then next very second its set to undefined.
console.log("stored value from db: "+hello);
http.get("api url", function(res) {
var body = '';
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
var parsed = JSON.parse(body);
var dataArray = [];
//I need access to mongoDB data right here and use it in some arithmetic operation
var p1 = parsed.data.example1 + hello[0].price;
//finally push data into array
dataArray.push(p1);
console.log(dataArray);
//pushing to socket channel - live
nsp.emit('live', dataArray);
});
});
}
型号
var mongoose = require('mongoose');
mongoose.connect('mongodb://username:pswd....');
var db = mongoose.connection;
//Product Schema
var ProductSchema = mongoose.Schema({
name:{
type: String
},
price:{
type: Number
},
unit:{
type:Number
},
type:{
type:String
}
});
var Product = module.exports = mongoose.model('Product', ProductSchema);
实现此目的的最佳方法是使用 Nodejs 缓存:https://www.npmjs.com/package/node-cache Or https://www.npmjs.com/package/node-file-cache
它只是将您想要的任何数据写入文件(只要您愿意,就可以存储为键值对)。设置、检索或更新值很容易。您还可以获取多个键值对。
我在 Nodejs 中创建了一个实时应用程序,我正在使用 socket.io 向给定频道中的所有连接用户广播数据。例如,我正在对 API-url 进行 http 调用:每 1 秒 http://example.com/api/token=tokenumber
并获取新的 json 数据。这些数据在推送到客户端查看之前需要做一个算术运算(为了进行算术运算,应用程序需要连接到数据库,如:mongo 或 mysql 并获取将要执行的数据用于计算)。
例如-工作流程
step 1) make http call [received data x=80, y=90]
step 2) connect to db [received data a=10, b=20]
step 3) do calculation [finalval = x*a + y*b]
step 4) push data to view
在这里,我们连接到数据库,因为那些 a 和 b 变量 是动态的,由管理员使用某种控制面板设置。
问题
目前,我每 1 秒连接一次数据库,因为我每 1 秒进行一次 http 调用。但我很确定 a 和 b 的值将是相同的,直到任何管理员用户登录到控制面板并更改这些值。 简而言之,资源浪费,我相信我通过每 1 秒连接到数据库并获得相同的未更改值来大量使用资源
我该如何优化这个?我如何确定数据库 table 中是否发生了任何更改,如果发生了任何更改,我如何获取新值并使用某种内存缓存存储它,以便节省时间和通过在值未更改时不连接到数据库来获取资源。
需要注意的一件事是,当数据库中的值发生变化时 table,它们应该立即反映出来,因为应用程序是实时的。
我知道每 1 分钟或 5 分钟检查一次数据库 table 的解决方案,以查明值是否已更改,具体取决于缓存的新值。但这意味着如果我在 t0 更改 a 和 b 的值,最坏的情况是在 1 或 5 分钟后更新视图,一旦执行到 db,我不希望这样。
更新
所以,我有我之前解释过的代码:我正在尝试将从 db 中提取的值保存到某个变量 X。但是在下一次调用时,它 被设置为未定义,因为函数测试是每 N 秒调用一次。但与数据库的连接只发生一次.
我只会在应用程序运行时调用数据库,每当管理员对数据库进行任何更改并点击提交按钮时我都会再次调用它。
我不想进行任何数据库调用,但在此之前使用从 Db 中提取的相同值。我尝试将它存储在一个变量中,但下一次调用发生时,同一个变量被设置为未定义。
代码
//requiring model products
var Product = require('./models/product');
//Here function is an async callback
Product.find(function (err, products) {
if (err) return console.error(err);
console.log("callback function results: "+products);
//I am passing data to test function once I receive the data from MongoDB
test(products);
});
//This function is called every N seconds
function test(val) {
//Here I am trying to store mongoDB data to variable hello
var hello = val;
//This prints only once when data from mongodb is recieved then next very second its set to undefined.
console.log("stored value from db: "+hello);
http.get("api url", function(res) {
var body = '';
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
var parsed = JSON.parse(body);
var dataArray = [];
//I need access to mongoDB data right here and use it in some arithmetic operation
var p1 = parsed.data.example1 + hello[0].price;
//finally push data into array
dataArray.push(p1);
console.log(dataArray);
//pushing to socket channel - live
nsp.emit('live', dataArray);
});
});
}
型号
var mongoose = require('mongoose');
mongoose.connect('mongodb://username:pswd....');
var db = mongoose.connection;
//Product Schema
var ProductSchema = mongoose.Schema({
name:{
type: String
},
price:{
type: Number
},
unit:{
type:Number
},
type:{
type:String
}
});
var Product = module.exports = mongoose.model('Product', ProductSchema);
实现此目的的最佳方法是使用 Nodejs 缓存:https://www.npmjs.com/package/node-cache Or https://www.npmjs.com/package/node-file-cache
它只是将您想要的任何数据写入文件(只要您愿意,就可以存储为键值对)。设置、检索或更新值很容易。您还可以获取多个键值对。