最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keep data for node app when alive - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Aug 17, 2015 at 16:18 Louis 151k28 gold badges284 silver badges328 bronze badges asked Aug 12, 2015 at 7:06 user4209821user4209821 5
  • It sounds like your current method caches the data for all sessions/users (Data stored per app instance). If this data is related to the session (data stored per session instance), you might want to use session data. I found this article, if it helps. codeforgeek.com/2014/09/manage-session-using-node-js-express-4 – Tom Pietrosanti Commented Aug 17, 2015 at 16:58
  • try node-cache for caching. I would use a proper database.npmjs.com/package/node-cache – DXM Commented Aug 20, 2015 at 22:19
  • Why not just save the data to a file after you have first parsed it. Use a local sessionID in the filename to keep it relevant to the app instance. Delete it when the app closes. – Blindman67 Commented Aug 21, 2015 at 13:25
  • I use memcached for this stuff. What's great is the cache can be shared across instances of my app, and it will survive beyond an app restart. – Josh C. Commented Aug 21, 2015 at 13:33
  • 1 If you just need a few things through the server's live time - just store them inside an Object. It's pretty simple and you don't have to use any kind of package. Just use a file, export one object and start putting things inside it :) – Andrey Popov Commented Aug 21, 2015 at 15:06
Add a comment  | 

3 Answers 3

Reset to default 27 +50

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.

Anything that your Node program loads into memory will remain accessible until the program stops. 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. You can load data from another file using require("./data.js"), where the contents of data.js must export your data:

module.exports = { ... };

From the docs:

Multiple calls to require('foo') may not cause the module code to be executed multiple times.

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.

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.


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.

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.

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:

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

You could use node-cache-manager with memory store:

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) {});
    });
});
发布评论

评论列表(0)

  1. 暂无评论