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

时间:2022-10-14 15:37:40

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表示节点

3 个解决方案

#1


4  

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. 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.

当您需要数据在Node应用程序运行时保持可用时。如果它停止或崩溃,数据将丢失。这是最容易解决的问题,我认为您在应用程序中寻找的级别。

Anything that your Node program loads into memory will remain accessible until the program stops. So if your program starts off with the line

在程序停止之前,Node程序加载到内存中的任何内容都将保持可访问状态。因此,如果您的程序从该行开始

var myData = { ... };

then myData will remain in memory and accessible from that file for as long as the application does. You can load data from another file using require("./data.js"), where the contents of data.js must export your data:

然后myData将保留在内存中,并且只要应用程序可以从该文件访问。您可以使用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. Your code simply places data directly inside that cache, without executing an external file.

要求缓存执行data.js的结果,并在每次随后请求时从缓存中提供它们。您的代码只是将数据直接放在缓存中,而不执行外部文件。

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. You don't need to explicitly cache it. If you've got multiple files, you should use the require('foo') function as intended.

如果你有一个文件,你可以随心所欲地重复使用变量myData - 它不会去任何地方。您不需要显式缓存它。如果你有多个文件,你应该按照预期使用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. 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实现此目的,您需要安装快速会话包。它非常易于使用。像任何其他模块一样包含它,告诉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 (e.g. MySQL, MongoDB, ...), and it becomes their responsibility to look after it. When you need the data again, even after a restart, you can simply ask them for it.

最常使用外部数据库解决此问题。 Node将您的数据传递到其他应用程序(例如MySQL,MongoDB,......),它们有责任照顾它。当您再次需要数据时,即使重启后,您也可以直接询问它们。

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. In this case, global persistence is as easy as:

您可以使用fs内置模块轻松完成此操作,或使用预制解决方案(如node-persist)。在这种情况下,全局持久性就像:

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

#2


2  

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 . Try it from here

我相信您想要在应用程序的服务器端缓存数据。如果是这种情况,则使用可以使用node-cache npm此外,还有一个名为node-persist的npm使用HTML5 localstorage功能的API。从这里试试吧

#3


0  

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) {});
    });
});

#1


4  

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. 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.

当您需要数据在Node应用程序运行时保持可用时。如果它停止或崩溃,数据将丢失。这是最容易解决的问题,我认为您在应用程序中寻找的级别。

Anything that your Node program loads into memory will remain accessible until the program stops. So if your program starts off with the line

在程序停止之前,Node程序加载到内存中的任何内容都将保持可访问状态。因此,如果您的程序从该行开始

var myData = { ... };

then myData will remain in memory and accessible from that file for as long as the application does. You can load data from another file using require("./data.js"), where the contents of data.js must export your data:

然后myData将保留在内存中,并且只要应用程序可以从该文件访问。您可以使用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. Your code simply places data directly inside that cache, without executing an external file.

要求缓存执行data.js的结果,并在每次随后请求时从缓存中提供它们。您的代码只是将数据直接放在缓存中,而不执行外部文件。

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. You don't need to explicitly cache it. If you've got multiple files, you should use the require('foo') function as intended.

如果你有一个文件,你可以随心所欲地重复使用变量myData - 它不会去任何地方。您不需要显式缓存它。如果你有多个文件,你应该按照预期使用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. 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实现此目的,您需要安装快速会话包。它非常易于使用。像任何其他模块一样包含它,告诉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 (e.g. MySQL, MongoDB, ...), and it becomes their responsibility to look after it. When you need the data again, even after a restart, you can simply ask them for it.

最常使用外部数据库解决此问题。 Node将您的数据传递到其他应用程序(例如MySQL,MongoDB,......),它们有责任照顾它。当您再次需要数据时,即使重启后,您也可以直接询问它们。

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. In this case, global persistence is as easy as:

您可以使用fs内置模块轻松完成此操作,或使用预制解决方案(如node-persist)。在这种情况下,全局持久性就像:

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

#2


2  

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 . Try it from here

我相信您想要在应用程序的服务器端缓存数据。如果是这种情况,则使用可以使用node-cache npm此外,还有一个名为node-persist的npm使用HTML5 localstorage功能的API。从这里试试吧

#3


0  

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) {});
    });
});