简体   繁体   English

在活动时保留节点应用程序的数据

[英]Keep data for node app when alive

I've node application and I need to keep data(as far the node app is alive) which will serve the user requests,(I dont want to calculate/parse it for every request so I do it once...) , when the node app is down I dont need this data anymore. 我是节点应用程序,我需要保留数据(到节点应用程序还活着),它将服务于用户请求,(我不想为每个请求计算/解析它,所以我这样做了一次......),当节点应用程序已关闭我不再需要此数据。

I found the following(which is working) but not sure is this is the best way to do so and what is the drawback if any. 我发现以下(这是有效的)但不确定这是最好的方法,如果有的话有什么缺点。

This is what I use which is working 这就是我使用的工作方式

require.cache.persist.myData = myData;

I use node with express 我用express表示节点

There are many ways one can make data persistent, depending on what level of persistence you need. 有许多方法可以使数据持久化,具体取决于您需要的持久性级别。 I'll outline a few approaches below. 我将在下面概述几种方法。


Application-level Persistence 应用程序级持久性

For when you need data to remain available for as long as the Node application is running . 当您需要数据在Node应用程序运行时保持可用时。 If it stops or crashes, the data is lost. 如果它停止或崩溃,数据将丢失。 This is the easiest problem to solve, and I think the level that you're looking for in your application. 这是最容易解决的问题,我认为您在应用程序中寻找的级别。

Anything that your Node program loads into memory will remain accessible until the program stops. 在程序停止之前,Node程序加载到内存中的任何内容都将保持可访问状态。 So if your program starts off with the line 因此,如果您的程序从该行开始

var myData = { ... };

then myData will remain in memory and accessible from that file for as long as the application does. 然后myData将保留在内存中,只要应用程序可以访问该文件 You can load data from another file using require("./data.js") , where the contents of data.js must export your data: 您可以使用require("./data.js")从其他文件加载数据,其中data.js的内容必须导出您的数据:

module.exports = { ... };

From the docs : 来自文档

Multiple calls to require('foo') may not cause the module code to be executed multiple times. 多次调用require('foo')可能不会导致模块代码多次执行。

require caches the results of executing data.js , and serves them from the cache each time they are subsequently requested. require缓存执行data.js的结果,并在每次随后请求时从缓存中提供它们。 Your code simply places data directly inside that cache, without executing an external file. 您的代码只是将数据直接放在缓存中,而不执行外部文件。

This does not carry any particular advantages over the strategies above. 与上述策略相比,这没有任何特别的优点。

If you've got one file, you may as well just reuse the variable myData as often as you like - it's not going anywhere. 如果你有一个文件,你可以随心所欲地重复使用变量myData - 它不会去任何地方。 You don't need to explicitly cache it. 您不需要显式缓存它。 If you've got multiple files, you should use the require('foo') function as intended. 如果你有多个文件,你应该按照预期使用require('foo')函数。


Session-level Persistence 会话级持久性

For when you need data to remain available from one request to another for some individual user. 当您需要数据从某个用户的一个请求保持可用到另一个请求时 Note that this is generally weaker than application-level persistence. 请注意,这通常比应用程序级别的持久性弱。 If the app crashes and restarts between two visits of a single user, the cached data may be lost. 如果应用程序在单个用户的两次访问之间崩溃并重新启动,则缓存的数据可能会丢失。 If this is problematic, see the final section of this answer. 如果这有问题,请参阅本答案的最后一部分。

To achieve this with express, you will need to install the express-session package. 要使用express实现此目的,您需要安装express-session包。 It's very easy to use. 它非常易于使用。 Include it like any other module, tell express to use it as middleware, and you'll find yourself with a persistent req.session object which you can store your data inside. 像任何其他模块一样包含它,告诉express使用它作为中间件,你会发现自己有一个持久的req.session对象,你可以将你的数据存储在里面。

For example: 例如:

var session = require('express-session');
var app = express();
app.use(session({secret: 'ssshhhhh'}));

app.get('/',function(req,res){
    req.session = myData;
}

Global Persistence 全球持久性

For when you need data to become available even after the application as stopped . 当您需要数据即使在应用程序停止后也可用时。 You can pick up where you left off when the application resumes. 您可以在应用程序恢复时从中断处继续。

This problem is most often solved using an external database. 最常使用外部数据库解决此问题。 Node passes your data onto some other application (eg MySQL, MongoDB , ...), and it becomes their responsibility to look after it. Node将您的数据传递到其他应用程序(例如MySQL,MongoDB ,......),它们有责任照顾它。 When you need the data again, even after a restart, you can simply ask them for it. 当您再次需要数据时,即使重启后,您也可以直接询问它们。

In many cases setting up a database just for persistence of data is unnecessary, however. 但是,在许多情况下,仅为持久化数据而设置数据库是不必要的。 It is easier to simply write your data to a local file , which you can trust to remain intact even if your application crashes. 将数据简单地写入本地文件更容易,即使应用程序崩溃,您也可以信任该文件保持不变。 In almost every case this will be much faster than using a database. 几乎在每种情况下,这都比使用数据库快得多。

You can easily do that yourself using the fs built-in module, or use a premade solution such as node-persist . 您可以使用fs内置模块轻松完成此操作,或使用预制解决方案(如node-persist In this case, global persistence is as easy as: 在这种情况下,全局持久性就像:

var storage = require('node-persist');
storage.initSync();
storage.setItem('myPersistentData', { ... });
console.log(storage.getItem('myPersistentData'));

i believe that you want to cache the data on the server side of your application. 我相信您想要在应用程序的服务器端缓存数据。 If this is the case then use can use node-cache npm Also, there is one more npm called node-persist that uses the HTML5 localstorage feature's API . 如果是这种情况,则使用可以使用node-cache npm此外,还有一个名为node-persist的npm使用HTML5 localstorage功能的API。 Try it from here 从这里试试吧

You could use node-cache-manager with memory store: 您可以将node-cache-manager与内存存储一起使用:

var cacheManager = require('cache-manager');
var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/});
var ttl = 5;
// Note: callback is optional in set() and del().

memoryCache.set('foo', 'bar', {ttl: ttl}, function(err) {
    if (err) { throw err; }

    memoryCache.get('foo', function(err, result) {
        console.log(result);
        // >> 'bar'
        memoryCache.del('foo', function(err) {});
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使应用程序和NodeJS之间的连接保持活动2分钟 - Keep the connection between app and NodeJS alive for 2 minutes 保持活动组件数据? Angular和动态元件? - Keep alive component data ? Angular and dynamic component? 即使未导航到Angular 2,Angular 2也会使路由保持活动状态 - Angular 2 keep route alive even when not navigated to it 如何在 node.js 中保持分叉子进程存活 - How to keep forked child process alive in node js Node.js:Express.js 设置保持活动状态 - Node.js: Express.js set keep-alive 节点进程结束后,Grunt(保持活动状态)没有退出 - Grunt (keep-alive) does not exit after the node process ends Node.js发布请求-使会话保持活动状态 - Node.js Post Request - Keep Session Alive 如何保留部署在谷歌云应用引擎上的 node.js 应用的统计数据并保持我的数据持久化 - How to keep stats for a node.js app deployed on google cloud app engine and keep my data persistent Greasemonkey可以在新页面上保存脚本数据和连接或重新加载它们吗? - Can Greasemonkey keep script data and connections alive on a new page or reload? 使用 Chrome 控制台移动到新 URL 时,有没有办法让变量保持活动状态? - When moving to new URL with Chrome console, is there a way to keep variables alive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM